vipin93
vipin93
1 year ago (61,280 XP)

try to use eager loading with('products.perr')..

bestmomo
bestmomo
1 year ago (356,570 XP)

In your controller something like that :

$treatment = Treatment::with('products')->find($treatment_id)
vipin93
vipin93
1 year ago (61,280 XP)

and first you should check value availability if not available then print null or something like $products->some->nnnn or 'mesg'

abombelli
$treatment = Treatment::with('products')->find($treatment_id)

Ok, with this I get the treatment with a collections of two associated products. But in the single edit view I need to additionally filter by product_id. Do I need to chain where clauses?

abombelli

Could you point me to a direction how I can eager load respecting both foreign keys in the pivot table?

abombelli

Ok, tried further and found a solution. But could this lines be optimised?

    public function edit_prescription($treatment_id, $product_id) {

        $treatment = Treatment::with(['products' => function ($query) use ($product_id) {
            $query->where('id', '=', $product_id);
        }])->where('treatments.id', $treatment_id)->first();

        $product = $treatment->products->first();
    
        return view('prescriptions.edit', compact('product', 'treatment') );
    }

Please sign in or create an account to participate in this conversation.