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.
>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.