17

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?

4 Answers 4

23

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?

Sign up to request clarification or add additional context in comments.

2 Comments

It's a core PHP feature. I doubt it's in the Laravel docs for that reason.
Ha.. really!? Shows what a PHP rookie I am. Thanks
4

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);
        }]);

Comments

1
$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();

Comments

0
$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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.