我试图创建一个计算用户总产品的查询,但是我的代码无效,我在代码上面留下了:
表: 画廊:
- 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
答案 0 :(得分:1)
如果您想要计算一个图库的产品,则需要获取该对象:
$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();
答案 1 :(得分:0)
$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]);