Support the ongoing development of Laravel.io →
Eloquent
Last updated 1 year ago.
0

This ^^

Also since you'll get a Collection back, you could sort it with some Laravel helpers (see docs) - however doing it inside the query will be more efficient.

Last updated 1 year ago.
0

Ehesp said:

This ^^

Also since you'll get a Collection back, you could sort it with some Laravel helpers (see docs) - however doing it inside the query will be more efficient.

But how do I do it in the query?

Last updated 1 year ago.
0

MaggieCabrera said:

But how do I do it in the query?

Maybe the Eager Load Constraints (towards the bottom of the Eager Loading section) will help with this.

Are you looking to do something like....?

// Returns collection of Teammembers ordered by last name with each
// Teammember's Hostess eager loaded.
$sortedMembers = Teammember::orderBy('lastname', 'desc')
    ->with('hostess')->get();

Or are you looking at how to do this on the inital Operation::find($id) query?

Last updated 1 year ago.
0

I don't care when to do it, but it seems like with my current method there is no place to do it and I need to change the way I do the query? I am very lost with this right now. I'd prefer if the Operation::find($id) was already sorted from the start, changing to eager loading means changing most of my code, right? Besides I've never used it, I'm barely starting with laravel

Last updated 1 year ago.
0

Change Operation.php content to

public function team()
{
    return $this->hasMany('Team')->orderBy('lastname', 'desc');
}
Last updated 1 year ago.
0

uldisrudzitis said:

Change Operation.php content to

public function team()
{
   return $this->hasMany('Team')->orderBy('lastname', 'desc');
}

I get:

    SQLSTATE[42S22]: Column not found: 1054 Unknown column 'lastname' in 'order clause' (SQL: select * from `teams` where `teams`.`operation_id` = 1 order by `lastname` desc) (View: /Users/Maggie/Sites/hotesses/laravel/app/views/operations/index.blade.php) 
Last updated 1 year ago.
0

uldisrudzitis said:

Change Operation.php content to

public function team()
{
   return $this->hasMany('Team')->orderBy('lastname', 'desc');
}

That returns the Operation's Teams ordered by lastname. Teams don't have a lastname, so that is the reason for the error.

I think uldisrudzitis meant something like the following.

Teammember.php

public function hostess()
{
    return $this->belongsTo('Hostess', 'hostess_id')->orderBy('lastname', 'desc');
}

But the above doesn't make sense because each Teammember belongsTo (meaning it is only related to one) Hostess. You'd have to sort the results as mentioned by Ehesp in the second post, unless you want to adjust your relationships. :/

Does the reasoning behind this make sense?

Last updated 1 year ago.
0

jlaswell said:

uldisrudzitis said:

Change Operation.php content to

public function team()
{
   return $this->hasMany('Team')->orderBy('lastname', 'desc');
}

That returns the Operation's Teams ordered by lastname. Teams don't have a lastname, so that is the reason for the error.

I think uldisrudzitis meant something like the following.

Teammember.php

public function hostess()
{
   return $this->belongsTo('Hostess', 'hostess_id')->orderBy('lastname', 'desc');
}

But the above doesn't make sense because each Teammember belongsTo (meaning it is only related to one) Hostess. You'd have to sort the results as mentioned by Ehesp in the second post, unless you want to adjust your relationships. :/

Does the reasoning behind this make sense?

Yes, I reached the same conclusion about trying to sort the models. Also sorting Operation::find($id) makes no sense either since I'm only querying operation, I only get the info from the members in the view, when I'm doing the foreach loops. I see why it won't work but I'm not sure how to approach the solution. It seems that I need to remake my query and make the joins in the controller? That way I can sort it there, but I wouldn't be using the model functions on the view then.

It seems like there should be a way to benefit from eloquent and also be able to sort everything out, but I'm not clear how...

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.