Laravel Classroom image

Prashant  Nigam / Student / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9|Last
Lessons:-Fluent Query Builder

10.3 Table Joins
Let’s have a look at a join using Fluent.
1 <?php
2
3 DB::table('tasks')
4 ->join('project', 'tasks.id', '=', 'project.task_id')
5 ->get(array('task.name', 'project.name'));We pass the name of the join table as the first parameter, and use the remaining three parameters
to perform an ‘ON’ clause, similar to how we perform ‘WHERE’s.
We then pass the fields we want to return to the get() method as an array.
We can do a left_join() exactly the same way, in fact it takes the same parameters, easy huh?
Do you remember the nested where clauses? Well you can use a similar technique to add more
conditions to the ON clause of a join. Let’s take a look.
1 <?php
2
3 DB::table('tasks')
4 ->join('project', function($join) {
5 $join->on('tasks.id', '=', 'project.task_id');
6 $join->or_on('tasks.author_id', '=', 'project.author_id');
7 })
8 ->get(array('task.name', 'project.name'));
In this case, we pass a closure as the second parameter to the join() method, then use the on(),
or_on() and and_on() methods to set the conditions.

 

 
 
 

Prashant  Nigam

Skills    Laravel

Qualifications :-
Location :-,,,
Description:-
Explore
 

  Students (0)