根据 laravel 背包中的用户登录获取数据

Get data based on user login in laravel backpack

我正在使用背包在我的项目中创建管理面板。我有两种类型的用户,一种是超级管理员,另一种是管理员。我只是想授予超级管理员权限,因为他可以列出、添加、编辑数据库中的所有行..

但管理员只能编辑、删除和列出自己创建的那些行..

所以请帮助我,我是 laravel 背包的新手??

过滤您在实体的 CrudController 的 setup() 方法中显示的结果,然后禁止访问 update/destroy 方法。

您的结果可能如下所示:

public function setup() 
{
    // take care of LIST operations
    if (\Auth::user()->hasRole('admin')) {
       $this->crud->addClause('where', 'author_id', '=', \Auth::user()->id);
    }
}

此外,您需要在 update()destroy() 方法中进行检查,以防止管理员删除其他人的条目。

// place this both inside your update() and inside your destroy() method 
if (\Auth::user()->hasRole('admin') && $this->crud->entry->author_id!=\Auth::user()->id) {
   abort(405);
}

希望对您有所帮助。