Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0

There are several ways to achieve the same.

$wanted = Posting::where('user_id', '=', $user_id)->whereHas('offers', function($offerQuery){
    $offerQuery->where('id', '=', $offer_id);
})->get();

You probably do some manipulation with the user

$user = User::find($user_id);
$wanted = $user->postings()->whereHas('offers', function($offerQuery){
    $offerQuery->where('id', '=', $offer_id);
})->get();

Or you inspect the offer

$offer= Offer::find($offer_id);
$wanted = $offer->postings()->where('user_id', '=', $user_id)->get();
Last updated 1 year ago.
0

Or you make @Firtzberg's last example into one query:

$wanted = $offer->where('offer_id', $offer_id)->postings()->where('user_id', '=', $user_id)->get();
Last updated 1 year ago.
0

I think, loading the images should work with ->with('images'):

$wanted = $offer->where('offer_id', $offer_id)->postings()->where('user_id', '=', $user_id)->with('images')->get();
Last updated 1 year ago.
0

Somehow, this forum doesn't let me edit the previous posts, so here goes the corrected version:

$wanted = Offer::where('offer_id', $offer_id)->postings()->where('user_id', '=', $user_id)->with('images')->get();
Last updated 1 year ago.
0

Thanks a lot for everyone's help. The only change I had to make was adding ->first() so an object was returned rather than a collection.

$wanted = Offer::where('offer_id', $offer_id)->first()->postings()->where('user_id', '=', $user_id)->with('images')->get();
Last updated 1 year ago.
0

This is a useful thread! Can anyone run this same example using morphedbymany relationship between Offers and Postings? I believe it should look almost identical except that eloquent will know to query the morph tables and join results.

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

scalerboy scalerboy Joined 25 Nov 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.