Php 如何使用join和where together-Laravel?
我想在两个表之间建立一个关系。所以我想带来status=1的博客。我该怎么做呢。 我的分类表看起来像Php 如何使用join和where together-Laravel?,php,laravel,eloquent,Php,Laravel,Eloquent,我想在两个表之间建立一个关系。所以我想带来status=1的博客。我该怎么做呢。 我的分类表看起来像 id|category_name|status| 1 |Animals | 0 2 |Education | 1 3 |Water | 0 id|category_id|title | description | 1 | 1 |New Post 1 | Post descriptio
id|category_name|status|
1 |Animals | 0
2 |Education | 1
3 |Water | 0
id|category_id|title | description |
1 | 1 |New Post 1 | Post description 1 |
2 | 2 |New Post 2 | Post description 1 |
3 | 3 |New Post 3 | Post description 1 |
4 | 2 |New Post 4 | Post description 1 |
我的博客表看起来像
id|category_name|status|
1 |Animals | 0
2 |Education | 1
3 |Water | 0
id|category_id|title | description |
1 | 1 |New Post 1 | Post description 1 |
2 | 2 |New Post 2 | Post description 1 |
3 | 3 |New Post 3 | Post description 1 |
4 | 2 |New Post 4 | Post description 1 |
如果“类别”表中的where('status',1),我如何列出博客?
请帮帮我。感谢您的帮助。您好,您可以使用Laravel中的查询生成器编写查询, 请参阅下面的代码
$whereData= [
['categories.status',1]
];
$getData = Blog::join("categories",'categories.id', '=', 'blogs.category_id')
->where($whereData)
->get();
以上代码已经过测试和使用
我希望它能帮助你
谢谢
$data = DB::table('categories')
->join('blog', 'blog.category_id', '=','categories.id')
->select('blog.*', 'categories.category_name')->where('categories.status',1)->get();
上面的代码可能会对您有所帮助。表示您想要一个类别状态为1的博客列表。是的,正如您所说。提供了以下解决方案,谢谢:)$getData=Blog::join(“categories”、'categories.id'、'='、'blogs.category_id')->where('categories.status',1)->orderBy('id',desc')->take(6)->get();这就是我如何获得最后6分的方法。此代码出现错误。我认为问题出在“orderBy('id','desc')”中,您应该尝试categories.id或blogs.id,而不是orderBy$getData=Blog::join('categories','categories.id','=','blogs.category_id')->where('categories.status',1)->orderBy('blogs.id','desc->take(6)->get();谢谢你用这种方法纠正它。