【发布时间】:2017-04-26 21:23:26
【问题描述】:
我正在尝试显示添加到管理员的所有帖子,并且只向登录用户显示自己的帖子。
这就是我在控制器中尝试的方法
public function index(Request $request)
{
$items = Item::where('author_id', Auth::user()->id)->orderBy('id','DESC')->with('author')->paginate(5);
return view('items.index',compact('items'))
->with('i', ($request->input('page', 1) - 1) * 5);
}
在模型中我有这种关系。 商品型号:
public function author()
{
return $this->belongsTo(User::class);
}
用户模型
public function posts()
{
return $this->hasMany(Item::class, 'author_id');
}
如果管理员已登录以查看所有帖子,我该如何进行此操作?我正在使用 Entrust ACL,现在不明白如何更改查询
【问题讨论】:
标签: laravel laravel-5.4