Reputation: 1068
Let's say I have 2 models: Car, Driver.
Car model:
<?php
class Car extends \Eloquent {
public function driver()
{
return $this->belongsTo('Driver');
}
}
Driver model:
<?php
class Driver extends \Eloquent {
public function car()
{
return $this->hasOne('Car');
}
}
How to query one or all cars that don't belong to any driver yet? How to check whether queried car belongs to any driver?
Upvotes: 1
Views: 7236
Reputation: 81187
Eloquent way:
$driverlessCars = Car::has('driver', '=', 0)->get();
// Collection of Car models
For more info check this out: Laravel check if related model exists
Upvotes: 3