jeudi 25 avril 2019

Call to undefined relationship - Laravel

I am using latest Laravel. I have method in the controller which selects from table and data from relational table, too:

$cars= Car::with(array('car_photos'=>function($query){
    $query->select('id');
}))->get();

Cars has many photos. Car model:

public function photos(){
    return $this->hasMany('App\Models\CarPhoto');
}

CarPhoto model:

public function car(){
    return $this->hasOne('App\Models\Car');
}

I can select all photos for a car with:

$car= Car::where('id', $id)->first();
$photos = $car->photos;

but with $cars= Car::with...., I am getting:

Call to undefined relationship [car_photos] on model [App\Models\Car].



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire