Laravel - Return Eloquent 模型中的自定义额外数据
Laravel - Return Custom Extra Data in Eloquent Model
我有一个这样的广告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');
}
然后在controller中进行这样的查询-
return Advertisement::get();
还有这个-
所以,我有一些额外的数据,这对我处理大数据时的更好性能不利。
那么,有没有什么办法可以去掉多余的部分,只得到我们需要的。
或者有什么方法可以在 Eloquent 模型中的 with
clouse 中进行自定义查询?
在此先感谢您的帮助。
在您的广告模型中,您可以添加:
public $appends = ['total_views'];
public function getTotalViewsAttribute(){
return $this->UserAdvertisementView->sum('total_view');
}
这将在发送到 JSON 时自动将 total_views
属性附加到您的 Advertisement
模型。
我有一个这样的广告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');
}
然后在controller中进行这样的查询-
return Advertisement::get();
还有这个-
所以,我有一些额外的数据,这对我处理大数据时的更好性能不利。
那么,有没有什么办法可以去掉多余的部分,只得到我们需要的。
或者有什么方法可以在 Eloquent 模型中的 with
clouse 中进行自定义查询?
在此先感谢您的帮助。
在您的广告模型中,您可以添加:
public $appends = ['total_views'];
public function getTotalViewsAttribute(){
return $this->UserAdvertisementView->sum('total_view');
}
这将在发送到 JSON 时自动将 total_views
属性附加到您的 Advertisement
模型。