[英]count total products from users galleries
我试图创建一个查询来计算用户的总产品查询,但是我的代码无法正常工作,我将代码放在上面:
桌子:画廊:
- id;
- title
Products
- id;
- gallery_id;
- title;
- price
型号:图库
public function products(){
return $this->hasMany(Product::class);
}
我的查询:
$totalProducts = Gallery::where('user_id', $userId)->products()->count();
错误:
Non-static method App\Gallery::products() should not be called statically, assuming $this from incompatible context
如果要计算一个图库的产品,则需要获取对象:
$totalProducts = Gallery::where('user_id', $userId)->first()->products()->count();
如果要计算用户的所有产品,则应首先添加HasManyThrough关系:
public function userProducts()
{
return $this->hasManyThrough('App\Product', 'App\Gallery');
}
然后计算产品:
User::find($id)->userProducts()->count();
$pastview = DB::table('tbl_field_data')
->where('id',$pid)
->select('tbl_field_data.*', 'tbl_field_data.view')
->first();
DB :: table('tbl_field_data')-> where('id',$ pid)-> update(['view'=> $ pastview-> view + 1]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.