dimanche 6 août 2017

Eager loading first image, but if null get first image from other table laravel

I try to eager load first image but if it is null or return no row, I want to eager load first image from other table.

I have Listings tables with hasMany relationship to Items table. Listings and Items have their own images table with hasMany relationship.

Listing Model

public function first_listing_image()
    {
        return $this->hasOne('App\Listing_image');
    }
public function items()
{
    return $this->hasMany('App\Item')
}

In controller

$listings = Listing::with('first_listing_image', 'items.first_item_image')->paginate(10);

Is it possible to eager load items.first_item_image only when first_listing_image return null?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2hzVTg4
via IFTTT

Aucun commentaire:

Enregistrer un commentaire