markstewie
markstewie

Reputation: 9587

Laravel Eloquent : with query parameters

I've finally worked out how to put together a complex query to get related models.

This is what my query currently looks like...

    $campaign = Campaign::find($campaign_id);
    $buyers = $campaign->buyers()->with('notes')->with(['emails' => function($q){
        $q->where('campaign_id', '13');
    }])->get();

The complex part is I'm trying to get entries from emails that have both a matching buyer_id & campaign_id. This query achieves exactly what I'm after in a pretty efficient way...

BUT... I can't work out how to pass in parameters to the with closure. At the moment I've hard coded the id 13 into the where query in the closure but I want it to be equal to $campaign_id passed in to the original function.

How do I do this?

Upvotes: 17

Views: 45207

Answers (4)

Akbar Noto
Akbar Noto

Reputation: 619

$fpr = FPR::with([
  'detail','of_permintaan.detail', 
  'of_permintaan.detail.pembelian'=>function($q)use($id){
       return $q->where('pembelian_id',$id);}
])->findOrFail($id);

Well, in case u need to pass it on its child-child relation, but i don't recomend it

Upvotes: 0

Jasim Juwel
Jasim Juwel

Reputation: 766

TRY With This

$mainQuery=StockTransactionLog::with(['supplier','customer','warehouse','stockInDetails'=>function($query) use ($productId){
            $query->with(['product'])->where('product_stock_in_details.product_id',$productId);
        },'stockOutDetails'=>function($query) use ($productId){
            $query->with(['product'])->where('product_stock_out_details.product_id',$productId);
        },'stockDamage'=>function($query) use ($productId){
            $query->with(['product'])->where('product_damage_details.product_id',$productId);
        },'stockReturn'=>function($query) use ($productId){
            $query->select('id','return_id','product_id');
            $query->with(['product'])->where('product_return_details.product_id',$productId);
        }]);

Upvotes: 4

JayminLimbachiya
JayminLimbachiya

Reputation: 1006

$latitude = $request->input('latitude', '44.4562319000');
$longitude = $request->input('longitude', '26.1003480000');
$radius = 1000000;

$locations = Locations::selectRaw("id, name, address, latitude, longitude, image_path, rating, city_id, created_at, active,
                     ( 6371 * acos( cos( radians(?) ) *
                       cos( radians( latitude ) )
                       * cos( radians( longitude ) - radians(?)
                       ) + sin( radians(?) ) *
                       sin( radians( latitude ) ) )
                     ) AS distance", [$latitude, $longitude, $latitude])
    ->where('active', '1')
    ->having("distance", "<", $radius)
    ->orderBy("distance")
    ->get();

Upvotes: 1

markstewie
markstewie

Reputation: 9587

Worked it out if anyone has same problem... need to use use statement

    $campaign = Campaign::find($campaign_id);
    $buyers = $campaign->buyers()->with('notes')->with(['emails' => function($q) use ($campaign_id){
        $q->where('campaign_id', $campaign_id);
    }])->get();

Is this documented anywhere?

Upvotes: 23

Related Questions