Is it possible to sort the result of the parent model by the child model?

Asked

Viewed 142 times

1

Controller

$menu       = MenuLoginPermission::with('submenu')
            ->where('id_empresa_login', '=', $idUser)
            ->get();

Model Menuloginpermission

class MenuLoginPermission extends Model
{
    public function menu(){
        return $this->belongsTo('App\Menu', 'id_menu', 'id');
    }
    public function submenu(){
        return $this->hasMany('App\Submenu', 'id_menu', 'id_menu');
    }
}

I’m not getting the Model Pai ordination through a field ORDER of Model Filho.

That’s possible ?

I tried with Wherehas and Eager Loading, but it didn’t work out.

As a last resort Leftjoin. But first I’d like to know if there’s any other way.

Model Pai = Menuloginpermission

Model Filho = Menu

Table Field Menu = Order

1 answer

3


Is there a way to sort with the collection result, that is to say, Collection class, with the command Sort

Example:

Code:

$collect = collect(['d' => 2, 'a' => 1, 'z' => 0]);
return $collect->sort(function($a, $b){
    return $a == $b ? 0: ($a > $b ? 1 : -1);
})->toArray();

Exit:

{ "z": 0, "a": 1, "d": 2}

In your code:

  MenuLoginPermission::with('submenu')
   ->where('id_empresa_login', '=', $idUser)
   ->get();
   ->sort(function($p1,$p2)
     {
        $a = $p1->menu->order;
        $b = $p2->menu->order;
        return $a == $b ? 0: ($a > $b ? 1 : -1);
     }, SORT_REGULAR, false)
    ->toArray();

The most natural way would be with leftJoin

MenuLoginPermission::with('submenu')
  ->where('id_empresa_login', '=', $idUser)
  ->leftJoin('submenu', 'submenu.id_menu','=','menuloginpermission.id_menu')  
  ->orderBy('submenu.order','asc')
  ->select('menuloginpermission.*')
  ->get();

There is a way to write an anonymous item with the command with, but the result of the ordering does not influence the main item.

  • 1

    I’ll test the method sort. And yes, the most natural way would be using the leftJoin.

  • One information, I do not know all fields of relations if by any chance does not match, let me know @Gumball!

  • 1

    In the case of Sort, there in the ordering variables, it would be $p1->menu->order and not submenu.

  • Well, it worked the way you wanted it to. However, no @foreach of my view I am using the $key to make an account. And as ordered the Collection, keeps the indexes, but it was to start from zero independent situations.

  • @Gumball, first of all your question has been answered, the foreach I don’t know what you’re doing, you don’t have that information in the question!

  • The question is not the foreach, rather that the function Sort sorts values, but keys remain. I just wanted it to start from 0 sorting independent... Now just because I didn’t say anything on the question gets those graces of not being able to answer... what policy is this ?

  • I didn’t say I would answer or not, I just said I didn’t have the information in your question, or did I miss it? if you want and can put in your question and edit so that I end, anger leads to nothing...

  • I made the counter in hand. Thank you for taking the doubt.

  • 1

    @Gumball I think we can read the http://answall.com/help/how-to-ask. Questions should have as much detail as possible, when this does not occur it is difficult to give an adequate answer. This is the policy of the site. Avoid behaviors that lead to complaints from users: http://meta.pt.stackoverflow.com/q/5379/101

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.