abombelli

abombelli

Member Since 1 Year Ago

Experience Points 2,260
Experience Level 1

2,740 experience to go until the next level!

In case you were wondering, you earn Laracasts experience when you:

  • Complete a lesson — 100pts
  • Create a forum thread — 50pts
  • Reply to a thread — 10pts
  • Leave a reply that is liked — 50pts
  • Receive a "Best Reply" award — 500pts
Lessons Completed 21
Lessons
Completed
Best Reply Awards 0
Best Reply
Awards
  • Start Your Engines Achievement

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • First Thousand Achievement

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • One Year Member Achievement

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • Two Year Member Achievement

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • Three Year Member Achievement

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • Four Year Member Achievement

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • Five Year Member Achievement

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • School In Session Achievement

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • Welcome To The Community Achievement

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • Full Time Learner Achievement

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • Pay It Forward Achievement

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • Subscriber Achievement

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • Lifer Achievement

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • Laracasts Evangelist Achievement

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • Chatty Cathy Achievement

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • Laracasts Veteran Achievement

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • Ten Thousand Strong Achievement

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • Laracasts Master Achievement

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • Laracasts Tutor Achievement

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • Laracasts Sensei Achievement

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • Top 50 Achievement

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

09 Sep
1 year ago

abombelli left a reply on How Create Nested Relationship Over Three Models

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

abombelli left a reply on How Create Nested Relationship Over Three Models

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

08 Sep
1 year ago

abombelli left a reply on How Create Nested Relationship Over Three Models

$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 left a reply on How Create Nested Relationship Over Three Models

Could you point me to the right direction? Don I do this from the model or the controller?

abombelli left a reply on How Create Nested Relationship Over Three Models

Yes. Treatment model looks like this:

class Treatment extends Model
{

    protected $fillable = ['name'];

    
    public function user() {

        return $this->belongsTo(User::class);

    }

    public function products() {

        return $this->belongsToMany(Product::class)->withPivot('consumption_interval', 'units_per_interval');
    }

}

abombelli left a reply on How Create Nested Relationship Over Three Models

Yes. The only difference is, in show I access the pivot over the foreach loop and in the edit view I just pass the $product and $treatment object. And both are missing the relationship.

abombelli left a reply on How Create Nested Relationship Over Three Models

Then I get an error: Trying to get property of non-object

abombelli left a reply on How Create Nested Relationship Over Three Models

In show I have:

@foreach ($treatment->products as $product)
    $product->pivot->units_per_interval
@endforeach 

Then for the edit view I have the following function in the TreatmentsController, where I pass the tratment and product id in the route Route::get('/treatments/{treatment}/prescription/{product}/edit', '[email protected]_prescription'):

public function edit_prescription(Treatment $treatment, Product $product) {
    return view('prescriptions.edit', compact('product', 'treatment') );
}

But somehow I do not get the attribute in the edit view via $product->pivot->units_per_interval

abombelli left a reply on How Create Nested Relationship Over Three Models

Of course! Thanks a lot. I searched to much ;-)

Now I can insert the values into the pivot table: $treatment->products()->updateExistingPivot($product->id, [ 'consumption_interval' => request('consumption_interval'), 'units_per_interval' => request('units_per_interval') ]);

Where I'm still stuck, is accessing the values in the view. The pivot attributes appear fine in the "show" view {{ $product->pivot->units_per_interval }}, but not when editing the attributes. Then the array is missing the pivot relationship. Any idea what I'm missing?

07 Sep
1 year ago

abombelli left a reply on How Create Nested Relationship Over Three Models

An addition: products are shared across treatments. Prescriptions are product/treatment specific.

abombelli left a reply on How Create Nested Relationship Over Three Models

A Treatment has many products, where each product has a different prescription (amount of intake a user needs).

The prescription can be different for the same product in another treatment.

I hope this clarifies.

abombelli started a new conversation How Create Nested Relationship Over Three Models

Hello

I'm completely new to Laravel and have the following situation:

I have the following Models:

  • Treatments; belongsToMany Products, belongsTo User
  • Products; belongsToMany Treatments, hasManyPrescriptions
  • Prescriptions; ??
  • Users; hasMany Treatments

Treatments can have many Products. Each associated Product to a Treatment can have a unique Prescription.

The tables are:

  • Users (id, name)
  • Treatments (id, name, user_id)
  • Products (id, name)
  • Product_Treatment (product_id, treatment_id)
  • Prescriptions (id, amount, product_id, treatment_id, user_id)

I manage to create the correct relations from products to each treatment. What I cannot figure out is how to relate the prescription to the product in the corresponding treatment. A product can belong to many treatments. If I use a normal pivot table for products_prescription, then this relation applies to all treatments. This should not be the case.

How should I setup the relationship, so I'm able to use this in blade? @foreach ($treatment->products as $product) {{ $product->name }} {{ $product->prescription->amount }} @endforeach

Thanks a lot for any hint or help. Aldo