DEV Community

Morcos Gad
Morcos Gad

Posted on

2

5 Relationship Tips - Laravel

My dear, I found this wonderful source https://www.youtube.com/watch?v=ijt10uTM8LY that shows us 5 Laravel Relationship Tips that are useful to us. They will make us happy in our projects and write our code. I will touch quickly on them, but visit the source and deepen it a lot

  • withDefault()

We have in the database some Category that do not have a parent, and we do not want to show the empty value. Here comes the withDefault()

Image description

Image description

Image description

  • Eager and Lazy Loading With Constraint

Image description

  • Querying Relationship Existence With Constraint

Let's single out some limitations on finding results using whereHas

Image description

  • Shorter Syntax to Query Relationship Existence

Image description

  • Eager Loading and Query Relationship Existence Combo

Image description

I hope you enjoyed the code and I wish you always a happy code .

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay