Laravel 5 Laravel 5.1有许多透视表
我有模型多对多的关系。我想通过透视表CategoryNews关联状态和类别 类别模型Laravel 5 Laravel 5.1有许多透视表,laravel-5,many-to-many,pivot-table,has-many-through,Laravel 5,Many To Many,Pivot Table,Has Many Through,我有模型多对多的关系。我想通过透视表CategoryNews关联状态和类别 类别模型 class Category extends Model { public function news() { return $this->belongsToMany('App\News'); } } 新闻模型 class News extends Model { public function categories() {
class Category extends Model
{
public function news() {
return $this->belongsToMany('App\News');
}
}
新闻模型
class News extends Model
{
public function categories()
{
return $this->belongsToMany('App\Category');
}
public function state()
{
return $this->belongsTo('App\State');
}
}
以及状态模型
class State extends Model
{
public function news() {
return $this->hasMany('App\News');
}
}
我想选择与cat_type=2的状态相关的新闻(从类别表)
我试过了
但是cat_型滤清器不工作。我也尝试过hasManyThrough,但我不知道如何用Pivot表实现它
请帮助有一个Laravel 5.5 composer软件包,可以执行多级关系(deep) 包装: 例如:
用户
→ 属于许多人→ <代码>角色→ 属于许多人→ <代码>权限
class User extends Model
{
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function permissions()
{
return $this->hasManyDeep(
'App\Permission',
['role_user', 'App\Role', 'permission_role'], // Pivot tables/models starting from the Parent, which is the User
);
}
}
需要定义外键的示例:
主持人: 不要删除我的答案 解决这个问题花了我两天的时间,我很高兴我找到了解决办法,我想让全世界都知道 请注意,我的答案被否决了,但你删除了它 问题的标题是关于:Laravel的hasManyThrough Pivot表,我的答案就是,在Pivot表上执行hasManyThrough等价的解决方案 这个问题已经三年了,我们承认没有人想要一个特定于这个问题的解决方案,因为我们都有自己的解决方案。 如果我是你,我更喜欢一般的答案
class User extends Model
{
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function permissions()
{
return $this->hasManyDeep(
'App\Permission',
['role_user', 'App\Role', 'permission_role'], // Pivot tables/models starting from the Parent, which is the User
);
}
}