HI这是我在Laravel 5.1中的第一个项目。我被困在一个laravel多对多关系,请大家帮忙 我有一个像laravel 5.1多对多关系
新闻
ID的表|标题|内容
类别
ID |标题| cat_type
和数据透视表
category_news
ID | category_id | news_id
和模型
class News extends Model
{
public function categories()
{
return $this->belongsToMany('App\Category');
}
}
class Category extends Model
{
public function news() {
return $this->belongsToMany('App\News');
}
}
我如何得到cat_type = 1的所有的新闻与它S的相关类别
请帮助
我试图
$news = News::whereHas('categories',
function($query){
$query->where('cat_type','2');
})
->where('publish','1')
->orderBy('created_at', 'desc')
->take(5)
->get();//latest news
它给出一个消息,但不是相对的泰德类 请帮 谢谢
弗朗切斯科Malatesta是的,但orderBy不工作 – sanu
你能更具体吗? :)我没有元素来帮助你,否则......你得到什么错误? _orderBy_只是跳过? _take()_方法有效吗? –
实际上是什么发生的是1)如果消息属于多个类别,它会显示同一消息的多个时间2)的不正常--thanks – sanu