Skip to main content
Premium Members Only
Join to unlock this tutorial and all of our courses.
Premium Tutorial

Laravel Dynamic Columns Table: Group by 2-Level Relationship

June 19, 2023
9 min read

Premium Video Tutorial

Premium Members Only

This advanced tutorial is available exclusively to Laravel Daily Premium members.

Premium membership includes:

Access to all premium tutorials
Video and Text Courses
Private Discord Channel
Moazam avatar

I use this in my model: protected $with ='client']; but I still get warning from debugbarabout N+1 query. It's asking me to add App\Models\User in the model whereas I already defined the relationship of client in the method:

public function client(): BelongsTo { return $this->belongsTo(User::class, 'user_id'); }

What am I doing wrong here?

Povilas Korop avatar

Maybe you need to have return $this->belongsTo(Client::class, 'user_id'); instead?

Hard to say without debugging the full code and where N+1 comes from, exactly.

Moazam avatar

Thank you Povilas for your suggestion. Client is a user and has a user role "client". I will try to post relevant code soon.

👍 1
Dmitro avatar

Hey, interestingly, what would you do if it were necessary to create a link between products based on the characteristic?

property

id title

property_values

id property_id title

product_properties

product_id property_values_id

and how to expand it further to make linking between products?

Moazam avatar

Use pivot table and $model->sync or $model->attach to attach the characteristics.

Dmitro avatar

https://ibb.co/yVGGRpr

I mean, how should I write an migration to get such a result.

Each property is a separate product, collected in one group according to its characteristics.

And I have a question, how organize migrations