Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to combine two condition in laravel elequent relation

i’m biginner in laravel, How can I combine these two conditions in the controller?
the main table is tasks that has forign key to four tables office,commander,status,priority.

$tasks=Task::where([['type',0]])->get();
$tasks=Task::with(['Office', 'Commander','Status','Priority'])->paginate(8);
return view('taskmanager.tasks.index',compact('tasks'));

Each of the two conditions in the first and second lines works fine on its own. I want to apply both together.
Tasks whose type value is zero, while the tasks table has a foreign key with the other four tables.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can combine the two conditions by chaining the where and with methods together.

$tasks = Task::where('type', 0)
    ->with(['Office', 'Commander', 'Status', 'Priority'])
    ->paginate(8);

return view('taskmanager.tasks.index', compact('tasks'));

Task::where('type', 0) filters the tasks where the type is 0. The with(['Office', 'Commander', 'Status', 'Priority']) part is eager loading the relationships, which can help reduce the number of queries to your database. Finally, paginate(8) will paginate the results, showing 8 tasks per page.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading