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

Abr*_*hin 2 php mysql query-builder laravel eloquent

我有一个像这样的广告雄辩模型-

<?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');
    }
}
Run Code Online (Sandbox Code Playgroud)

所以它与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'
                                ];
}
Run Code Online (Sandbox Code Playgroud)

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

return Advertisement::with('UserAdvertisementView')->get();
Run Code Online (Sandbox Code Playgroud)

得到这样的东西——

[
  {
    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
      }
    ]
  }
]
Run Code Online (Sandbox Code Playgroud)

但我喜欢有这样的东西——

[
  {
    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
    ]
  }
]
Run Code Online (Sandbox Code Playgroud)

所以,我想有SUMtotal_count (2+1 = 3) 所有用户。

所以,我需要在Eloquent Model 中创建一个自定义查询。

有什么办法吗?


更新-

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

public $appends = ['total_views'];

public function getTotalViewsAttribute()
{
    return $this->UserAdvertisementView->sum('total_view');
}
Run Code Online (Sandbox Code Playgroud)

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

return Advertisement::get();
Run Code Online (Sandbox Code Playgroud)

还有这个——

在此处输入图片说明

所以,当我处理大数据时,我有一些额外的数据,这对更好的性能不利。

那么,有什么方法可以去除多余的部分并获得我们需要的东西。

或者有什么方法可以在withEloquent 模型的 clouse 中自定义查询?


提前感谢您的帮助。

Jef*_*eff 6

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

public $appends = ['total_views'];

public function getTotalViewsAttribute(){
  return $this->UserAdvertisementView->sum('total_view');
}
Run Code Online (Sandbox Code Playgroud)

当它被发送到 JSON 时,这将自动将total_views属性附加到您的Advertisement模型。