【问题标题】:How to use alias in laravel eager loading如何在 laravel 急切加载中使用别名
【发布时间】:2018-01-27 23:59:24
【问题描述】:

我想在 laravel 急切加载中使用别名。这是我的查询

Message::whereIn('conversation_id',$msgs)
        ->where('deleted', '!=', $userId)
        ->with(array('last_sender' =>function($query) use ($userId){
                $query->where('id','!=', $userId);
                $query->select('id', 'userName', 'profilePic','firstName', 'lastName');
          }))

         ->with(array('last_reciever' =>function($query) use ($userId){
                $query->where('id','!=', $userId);
                $query->select('id', 'userName', 'profilePic','firstName', 'lastName');
          }))
         ->orderBy('conversation_id', 'desc') 
         ->orderBy('id', 'desc')
         ->groupBy('conversation_id')
        ->get();

此查询返回用户对象,因为last_sender 和last_reciever 保持为空,或者last_reciever 和last_sender 保持为空。 这是此查询返回的结果对象

{
"data": [
    {
        "id": 85,
        "conversation_id": 18,
        "last_reciever": null,
        "last_sender": {
            "id": 39,
            "userName": "df76236sd",
            "profilePic": "aFHZmeMHQ6nd3Ll3nzCqYEw2CTBsun3f1hxm3yKc.jpeg",
            "firstName": "sadek ",
            "lastName": "sdfsd"
        },
        "msg": "abe sala",
        "attachment": null,
        "deleted": 0,
        "seen": 0,
        "created_at": "2017-08-19 02:24:05",
        "updated_at": "2017-08-19 02:24:05"
    },
    {
        "id": 80,
        "conversation_id": 16,
        "last_reciever": {
            "id": 39,
            "userName": "df76236sd",
            "profilePic": "aFHZmeMHQ6nd3Ll3nzCqYEw2CTBsun3f1hxm3yKc.jpeg",
            "firstName": "sadek ",
            "lastName": "sdfsd"
        },
        "last_sender": null,
        "msg": "now should be working just fine",
        "attachment": null,
        "deleted": 0,
        "seen": 1,
        "created_at": "2017-08-19 02:19:34",
        "updated_at": "2017-08-19 02:19:34"
     }
   ]
  }

我想要做的是用自定义名称 oponent 替换 last_sender 和 last_reciever 。为此,我尝试使用类似的东西

Message::whereIn('conversation_id',$msgs)
        ->where('deleted', '!=', $userId)
        ->with(array('last_sender as oponent' =>function($query) use ($userId){
                $query->where('id','!=', $userId);
                $query->select('id', 'userName', 'profilePic','firstName', 'lastName');
          }))

         ->with(array('last_reciever as oponent' =>function($query) use ($userId){
                $query->where('id','!=', $userId);
                $query->select('id', 'userName', 'profilePic','firstName', 'lastName');
          }))
         ->orderBy('conversation_id', 'desc') 
         ->orderBy('id', 'desc')
         ->groupBy('conversation_id')
        ->get();

而且它不起作用。有没有其他方法可以实现这一目标? 谢谢。

【问题讨论】:

    标签: laravel eloquent laravel-5.4 eager-loading


    【解决方案1】:

    您可以使用 toArray() 覆盖模型中的值

        public function toArray()
        {
            return [
               'oponent' => $this->last_sender. " " .$this->last_reciever,
            ];
        }
    

    或者使用 laravel api 资源

        class User extends JsonResource
    {
        /**
        * Transform the resource into an array.
        *
        * @param  \Illuminate\Http\Request
        * @return array
        */
        public function toArray($request)
        {
            return [
               'id' => $this->id,
               'name' => $this->name,
               'email' => $this->email,
               'created_at' => $this->created_at,
               'updated_at' => $this->updated_at,
           ];
        }
    }
    

    如果你想使用 laravel api 资源返回一个集合

    Route::get('/user', function () {
        return UserResource::collection(User::all());
    });
    

    https://laravel.com/docs/5.6/eloquent-resources

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-13
      • 2021-04-23
      • 2018-10-15
      • 1970-01-01
      • 1970-01-01
      • 2017-01-25
      • 2021-09-29
      相关资源
      最近更新 更多