TP 5跨表一对多 Has Many Through怎么反绑查数据

浏览:1892 发布日期:2018/04/11 分类:求助交流
本来可以先通过Conuntry查User,然后通过User查Post,现在你可以通过Country直接查Post;
通常Country和User,User和Post的表间关系都是事先建立好的,这个时候你再使用hasManyThrough;

表结构:
country
id - integer
name - string

user
id - integer
country_id - integer
name - string

post
id - integer
user_id - integer
title - stringnamespace App;

use Illuminate\Database\Eloquent\Model;

class Country extends Model
{

    public function posts()
    {
        return $this->hasManyThrough('Post', 'User', 'country_id', 'user_id');
        
    }
}
调用:$country = Country::get(2);
$posts = $country->posts();
这样就取得id=2 ,国家所有的文章。但有哪位高人知道,怎么反绑,通过某个文章 id=100的所属哪个国家???

最佳答案
评论( 相关
后面还有条评论,点击查看>>