Laravel - 在Eloquent模型中返回自定义额外数据

时间:2016-07-05 03:31:02

标签: php mysql laravel eloquent query-builder

我有这样的广告Eloquent Model -

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Advertisement extends Model
{
    protected $dates        =   ['deleted_at'];

    protected $table        =   'advertisements';           //Table Name

    protected $fillable     =   [
                                    'user_id',
                                    'category_id',
                                    'sub_category_id',
                                    'title',
                                    'price',
                                    'description',
                                    'address',
                                    'location_lat',
                                    'location_lon',
                                    'is_active',
                                    'deleted_at'
                                ];

    protected $hidden = [
                            'user_id',
                            'category_id',
                            'sub_category_id',
                            'deleted_at',
                            'created_at',
                            'updated_at',
                            'is_active',
                        ];

    public function User()
    {
        return $this->belongsTo('App\User','user_id', 'id');
    }

    public function UserAdvertisementView()
    {
        return $this->hasMany('App\UserAdvertisementView', 'add_id', 'id');
    }
}

因此它与UserAdvertisementView Eloquent Model相关联。所以,UserAdvertisementView就像这样 -

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class UserAdvertisementView extends Model
{
    protected $primaryKey   =   null;
    public $incrementing    =   false;
    public $timestamps      =   false;

    protected $table        =   'user_add_views';           //Table Name

    protected $fillable     =   [
                                    'user_id',
                                    'add_id',
                                    'total_view'
                                ];
}

所以,当我在控制器中使用它时 -

return Advertisement::with('UserAdvertisementView')->get();

得到这样的东西 -

[
  {
    id: 1,
    title: "as dasd as",
    price: 0,
    description: "asd asdasdasd",
    address: "Khagrachhari, Chittagong Division, Bangladesh",
    location_lat: 23.13,
    location_lon: 91.95,
    user_advertisement_view: [
      {
        user_id: 1,
        add_id: 1,
        total_view: 1
      },
      {
        user_id: 16,
        add_id: 1,
        total_view: 2
      }
    ]
  }
]

但我喜欢这样的事情 -

[
  {
    id: 1,
    title: "as dasd as",
    price: 0,
    description: "asd asdasdasd",
    address: "Khagrachhari, Chittagong Division, Bangladesh",
    location_lat: 23.13,
    location_lon: 91.95,
    user_advertisement_view: [
      total_view: 3
    ]
  }
]

因此,我想为所有用户提供total_count (2+1 = 3) SUM

因此,我需要在Eloquent Model

中创建自定义查询

有什么办法吗?

更新 -

根据@Jeff的说法,我已将此添加到Advertisement -

public $appends = ['total_views'];

public function getTotalViewsAttribute()
{
    return $this->UserAdvertisementView->sum('total_view');
}

然后在控制器中进行了这样的查询 -

return Advertisement::get();

有了这个 -

enter image description here

所以,我正在使用一些额外的数据,这些数据在我处理大数据时对于更好的性能不利。

那么,是否有任何方法可以删除额外的部分并获得我们需要的东西。

或者有没有办法在Eloquent Model的with clouse中进行自定义查询?

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

在广告模型上,您可以添加:

public $appends = ['total_views'];

public function getTotalViewsAttribute(){
  return $this->UserAdvertisementView->sum('total_view');
}

total_views模型发送到JSON时,会自动将Advertisement属性附加到<{1}}模型。