【问题标题】:Laravel - How to orderBy eloquent query with pivot and each functionLaravel - 如何通过具有枢轴和每个功能的雄辩查询来订购
【发布时间】:2016-11-15 13:21:52
【问题描述】:

我在更复杂的查询中遇到了 orderBy 的问题。

我想从指定的俱乐部中获取 10 个有序用户(以俱乐部为中心)并隐藏一些列。

所以我有一个雄辩的查询,这几乎对我有用。问题在于通过某些变量(级别或 exp - 尚不知道)对用户排序

我的查询(工作 - 没有排序):

        $users = Club::find($club_id)->users->each(function($row) {

            $row->setHidden(['history', 'books', 'reviews', 'exp', 'erudion_id', 'phone', 'bio', 'active', 'book_transfer_type', 'email', 'password', 'remember_token', 'created_at', 'updated_at', 'active_club', 'facebook_id', 'password_token', 'points', 'password_status', 'pivot']);

        })->take(10);

    return $users;

我的一个试验(使用 OrderBy):

    //
//        $users = Club::find($club_id)->with(['users' => function ($q) {
//
//            $q->orderBy('id');
//
//        }])->each(function($row) {
//
//            $row->setHidden(['history', 'books', 'reviews', 'exp', 'erudion_id', 'phone', 'bio', 'active', 'book_transfer_type', 'email', 'password', 'remember_token', 'created_at', 'updated_at', 'active_club', 'facebook_id', 'password_token', 'points', 'password_status', 'pivot']);
//
//        });
//
//        return $users;

错误:

响应内容必须是实现__toString()的字符串或对象,“boolean”给定。

我在我的模型中附加了属性。这个属性不能通过不包含在select中来取消选中,所以基本上都是选中的。

我想创建一个雄辩的查询,它将属性限制在少数,这是我感兴趣的。

【问题讨论】:

    标签: php laravel eloquent


    【解决方案1】:

    你可以试试:

    $users = Club::where('id', $club_id)
                    ->first()
                    ->users()
                    ->select('id', 'name') // you can add more columns which you want to select
                    ->orderBy('id')
                    ->get()
    
    return $users;
    

    更新

    或者如果你想使用each,那么你可以这样做:

    $users = Club::where('id', $club_id)
                    ->first()
                    ->users()
                    ->orderBy('id')
                    ->get()
                    ->each(function($user) {
                        $user->setHidden(['history', 'books', 'reviews', 'exp', 'erudion_id', 'phone', 'bio', 'active', 'book_transfer_type', 'email', 'password', 'remember_token', 'created_at', 'updated_at', 'active_club', 'facebook_id', 'password_token', 'points', 'password_status', 'pivot']);
                    });
    
    return $users;
    

    【讨论】:

    • 是的,但是我在隐藏一些属性时遇到了问题。在模型中,我有很多附加属性,选择总是显示。每个函数都不能处理这个查询:->each(function($row) { $row->setHidden(['history', 'books', 'reviews', 'exp', 'erudion_id', 'phone', 'bio', 'active', 'book_transfer_type', 'email', 'password', 'remember_token', 'created_at', 'updated_at', 'active_club', 'facebook_id', 'password_token', 'points', 'password_status', 'pivot']); });
    【解决方案2】:

    好的,我以不同的方式解决了这个问题。也许它看起来不太好,但它确实有效。工作速度比没有隐藏属性快得多。

    我在我的模型中使用附加属性,这些属性总是被选中(即使没有在 eloquent 中选择它们),所以我必须创建包含我感兴趣的属性的数组。

      $users = Club::where('id', $club_id)->first()->users()->orderBy('id')->limit(10)->get();
    
        $ranking = [];
    
        foreach($users as $k => $user){
            $ranking[$k]['id'] = $user['id'];
            $ranking[$k]['f_name'] = $user['f_name'];
            ...
        }
    
        return $ranking;
    

    谢谢大家提供线索。

    【讨论】:

      猜你喜欢
      • 2017-05-24
      • 1970-01-01
      • 2019-07-24
      • 2020-10-26
      • 2018-05-19
      • 2020-06-28
      • 1970-01-01
      • 2019-03-14
      • 1970-01-01
      相关资源
      最近更新 更多