Captain flapjack
Captain flapjack

Reputation: 428

Laravel 4 & Eloquent - returning related products

Pretty sure I'm asking too much of Eloquent, but anyway...

I have a model called 'Products' which is related to a model called 'Images'.

The 'Products' model contains this:

public function images()
{
    return $this->hasMany('Image');
}

The 'Images' model contains this:

public function product()
{
    return $this->belongsTo('Product');
}

I can return related images by using:

Product::with('images')

At this point everything works perfectly.

Now...

If the 'Images' model was related to a model called 'Trumpets', which was not related to 'Products', is it possible to somehow return everything in a way similar to:

Product::with('images', 'trumpets')

I'm aware of eager loading, but that's not what I need at this time unfortunately.

What I really need is a way to return related data beyond the relationship of the model I'm using.

Like I said, pretty sure it's asking too much, but any help would be appreciated.

Upvotes: 1

Views: 429

Answers (2)

Phill Sparks
Phill Sparks

Reputation: 20879

Do you mean this..?

Product::with('images', 'images.trumpets')

Upvotes: 4

Niclas Schumacher
Niclas Schumacher

Reputation: 232

im still in the learning process myself, but i assume you could do like

$product->images->trumpets()

Upvotes: -1

Related Questions