One of the benefit of using laravel is that you can do eager loading relationship in the model when using eloquent, in this article i'm going to show example of eager load in laravel.
For this tutorial i'm going to use two tables, cars and car_details:
table cars:
Schema::create('cars', function($table)
{
$table->bigIncrements('car_id');
$table->string('model_name', 255)->nullable();
$table->string('manufacture', 255)->nullable();
$table->timestamps();
});
table car_details
Schema::create('car_details', function($table)
{
$table->bigIncrements('car_detail_id');
$table->bigInteger('car_id')->nullable();
$table->string('year', 255)->nullable();
$table->string('color', 255)->nullable();
$table->timestamps();
});
Sample data
INSERT INTO `cars` (`car_id`, `model_name`, `manufacture`, `created_at`, `updated_at`) VALUES
(1, 'Pajero', 'Mitsubishi', '2017-05-03 00:08:21', '2017-05-03 00:30:54'),
(2, 'NSX', 'Honda', '2017-05-03 00:09:07', '2017-05-03 00:09:07'),
(3, 'F50', 'Ferrari', '2017-05-03 01:25:22', '2017-05-03 01:25:22');
INSERT INTO `car_details` (`car_detail_id`, `car_id`, `year`, `color`, `created_at`, `updated_at`) VALUES
(1, 1, '1996', 'blue', '2017-05-03 00:08:21', '2017-05-03 00:08:21'),
(2, 2, '2004', 'green', '2017-05-03 00:09:07', '2017-05-03 00:09:07'),
(3, 3, '2000', 'red', '2017-05-03 20:06:52', '2017-05-03 20:06:52');
Between cars and car_details the relationship is one to one, so in the Car model (car.php) we define a method named 'details' which can be called later in the query.
Car.php (car model)
<?php
class Car extends Eloquent {
protected $table = 'cars';
protected $primaryKey = 'car_id';
protected $fillable = array('model_name', 'manufacture');
public function details() {
return $this->hasOne('CarDetail');
}
}
CarDetail.php (car detail model)
<?php
class CarDetail extends Eloquent {
protected $table = 'car_details';
protected $primaryKey = 'car_detail_id';
public function car() {
return $this->belongsTo('Car');
}
}
On the query, you can use with() method to call the relationship, here's an example querying car data and also load the relationship with car detail.
$car = Car::with('details')->get();
print_r($car->toArray());
$car_detail = CarDetail::with('car')->get();
print_r($car_detail->toArray());
If you view the result on postman, you can see more clear the data structure returned by the query above.
Array
(
[0] => Array
(
[car_id] => 1
[model_name] => Pajero
[manufacture] => Mitsubishi
[created_at] => 2017-05-03 08:08:21
[updated_at] => 2017-05-03 08:30:54
[details] => Array
(
[car_detail_id] => 1
[car_id] => 1
[year] => 1996
[color] => blue
[created_at] => 2017-05-03 08:08:21
[updated_at] => 2017-05-03 08:08:21
)
)
[1] => Array
(
[car_id] => 2
[model_name] => NSX
[manufacture] => Honda
[created_at] => 2017-05-03 08:09:07
[updated_at] => 2017-05-03 08:09:07
[details] => Array
(
[car_detail_id] => 2
[car_id] => 2
[year] => 2004
[color] => green
[created_at] => 2017-05-03 08:09:07
[updated_at] => 2017-05-03 08:09:07
)
)
[2] => Array
(
[car_id] => 3
[model_name] => F50
[manufacture] => Ferrari
[created_at] => 2017-05-03 09:25:22
[updated_at] => 2017-05-03 09:25:22
[details] => Array
(
[car_detail_id] => 3
[car_id] => 3
[year] => 2000
[color] => red
[created_at] => 2017-05-04 04:06:52
[updated_at] => 2017-05-04 04:06:52
)
)
)
Besides hasOne and belongsTo, there are others relationship that you can use in laravel, i'll cover that in the next article.
EmoticonEmoticon