1

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?

Allan Tatter
  • 1,068
  • 1
  • 10
  • 15

1 Answers1

3

Eloquent way:

$driverlessCars = Car::has('driver', '=', 0)->get();
// Collection of Car models

For more info check this out: Laravel check if related model exists

Community
  • 1
  • 1
Jarek Tkaczyk
  • 78,987
  • 25
  • 159
  • 157