【发布时间】:2020-05-14 21:02:03
【问题描述】:
我正在接近 Laravel 7,我猜我迷失在平庸之中。我在模型中创建了一个函数,用于计算一个类别有多少帖子,然后传递 ID 参数。
我在控制器中调用这个函数,然后控制器会在视图中获取变量。
它给了我一个错误:
类 Illuminate\Database\Eloquent\Builder 的对象不能是 转成字符串
我该如何解决?
型号
public function scopeCountActivePostCategory($id)
{
$query = DB::table('post')->where([
['status', 1],
['category', $id],
]);
return $query->count();
}
控制器
// get details category by slug
// preleva dettagli categoria by slug
$detailCat = DB::table('categories')->where('slug', $slug)->first();
// get stories
$post = DB::table('post')
->orderByDesc('id')
->where('category', $detailCat->id)
->where('status', 1)
->paginate($set->app_result_x_page);
return view('category')->with([
'posts' => $posts,
'set' => $set,
'totalActivePost' => Post::CountActivePostCategory($detailCat->id),
'detailCat' => $detailCat
]);
【问题讨论】: