Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here
Premium Members Only
Join to unlock this tutorial and all of our courses.
Tutorial 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

Comments & Discussion

M
Moazam ✓ Link copied!

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?

PK
Povilas Korop ✓ Link copied!

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.

M
Moazam ✓ Link copied!

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

D
Dmitro ✓ Link copied!

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?

M
Moazam ✓ Link copied!

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

D
Dmitro ✓ Link copied!

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