1

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.

2
  • 1
    mean you want blog list which category status=1..? Commented Jan 11, 2020 at 5:00
  • Yes like you said. below solution was provided thanks :) Commented Jan 11, 2020 at 5:07

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

$getData = Blog::join("categories",'categories.id', '=', 'blogs.category_id') ->where('categories.status',1) ->orderBy('id', 'desc')->take(6)->get(); This is how I can get the last 6 . This code is giving an error.
i think issue is in " orderBy('id', 'desc') " , you should try categories.id or blogs.id instead of 'id' in orderBy
$getData = Blog::join("categories",'categories.id', '=', 'blogs.category_id') ->where('categories.status',1) ->orderBy('blogs.id', 'desc')->take(6)->get(); Thanks for trying it corrected this way.
0
$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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.