I have a users table and an appointments table. In appointment table I have two user ID's (customer_id, staff_id). I want to retrieve all the appointments with customer name and the staff name.
users table
id
name
appointments table
id
staff_id(user_id)
customer_id(user_id)
datetime
As you can see, I have to join the users table twice with the appointments table. Usually I do this with inner joins.
Can we do the same thing with Laravel eloquent eager loading using with()?
Can we do something like:
appointments::with('users' * )->get();?
* Do something here to inner join users table twice, and read user1.name as staff_name, user2.name as customer_name.
This is the final output I need:
appointment_id
staff_id
staff_name
customer_id
customer_name
datetime
I have another question, what is the second parameter in the following query?
User::with(array(
'post'=> function() use $region {
//what is use $region means? Can you give me an example?
}
));
Thanks!
Laravel Eloquent Join. Laravel JOIN eloquent returns all rows from the both table, if there are matches in the both table. Otherwise, the result is NULL. Now, demonstrates laravel eloquent join with the following examples. You can see the following example of laravel eloquent join() method: Example 1: Laravel Eloquent Join() with 2 Tables
Note that, you can also join two or multiple tables using laravel left, rigth and cross join. Laravel JOIN eloquent returns all rows from the both table, if there are matches in the both table. Otherwise, the result is NULL.
Laravel’s ORM, called Eloquent, makes it trivial to eager load models, and even eagerly loading nested relationships. Let’s build on the Post model example and learn how to work with eager loading in a Laravel project.
Laravel’s ORM, called Eloquent, makes it trivial to eager load models, and even eagerly loading nested relationships. Let’s build on the Post model example and learn how to work with eager loading in a Laravel project. We will work with the project setup and then go through some eager loading examples more in-depth to wrap up.
class T1 extends Eloquent {
protected $table = 't1';
}
class T2 extends Eloquent {
protected $table = 't2';
public function customer()
{
return $this->belongsTo('T1','c_id');//c_id - customer id
}
public function staff()
{
return $this->belongsTo('T1','s_id');//s_id - staff id
}
}
With use "with":
$list = \T2::with('customer')->with('staff')->get();
foreach ($list as $row) {
echo 'ID: '.$row->id.', customer: '.$row->customer->name.', staff: '.$row->staff->name.'<br>';
}
With joins:
$list = \T2::leftJoin('t1 as customer_table', 'customer_table.id','=','t2.c_id')
->leftJoin('t1 as staff_table', 'staff_table.id','=','t2.s_id')
->select('staff_table.name as staff_name','customer_table.name as customer_name')
->get();
foreach ($list as $row) {
echo 'customer: '.$row->customer_name.', staff: '.$row->staff_name.'<br>';
}
About second question - This is for subqueries. Look documentation: http://laravel.com/docs/eloquent#eager-loading
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With