ufuk
ufuk

Reputation: 371

How can I use join and where together - Laravel?

I want to establish a relationship between two tables. So I want to bring blogs with status = 1. How can I do that. My categories table looks like

id|category_name|status|
1 |Animals      |   0     
2 |Education    |   1     
3 |Water        |   0    

My blogs table looks like

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  |

How do I list blogs provided that where('status', 1) from categories table? Please help me. thanks for your help.

Upvotes: 1

Views: 220

Answers (2)

Vinit Joshi
Vinit Joshi

Reputation: 91

Hi you can write query with query builder in Laravel, Please see below code


$whereData= [
  ['categories.status',1]
];

$getData = Blog::join("categories",'categories.id', '=', 'blogs.category_id')
->where($whereData)
->get();

Above code is tested and worked.

I hope it will help you.

Thanks.

Upvotes: 1

vaishali tilala
vaishali tilala

Reputation: 1

$data = DB::table('categories')
->join('blog', 'blog.category_id', '=','categories.id')
->select('blog.*', 'categories.category_name')->where('categories.status',1)->get();

Above code may help you.

Upvotes: 0

Related Questions