inmn

Should I use Model Relationships vs Query Scopes and a simple mysql join?

Posted 9 months ago by inmn

Perhaps I am missing something; but when using .. belongsTo('App\Org') or $members = return $this->hasMany('App\Member'); models, I am finding that I often just return more than I need.

Ie:

If an Org has Members; $org->with('members'); gives me

Org

  • Members

If i want user data I have to do

$org->with('members','members.user');

Which gives me

Org

  • Members
    • User Record

If the user has any related fields, like contact numbers I am then working with

Org

  • Members
    • User Record
      • Related Models of User Record
        • Maybe even more

When returning public json output, i then have to clean up many of the fields being returned

(still learning how only return PARTS of the related models)

So with a query scope / custom mysql i could easily return a flat array of selected data for the memberDataOf($org)

What would you recommend?

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

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.