gpt4 book ai didi

php - 无法为用户分配角色

转载 作者:行者123 更新时间:2023-12-03 22:54:04 25 4
gpt4 key购买 nike

我试图在我的 Laravel 应用程序上使用 spatie(laravel-permission),但我无法为用户分配角色。我面临的错误是

调用未定义的方法 Illuminate\Database\Query\Builder::assignRole()

管理员 Controller .php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Traits\HasRoles;
class AdminsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$users = User::all();

return view('admin.dashboard', compact('users'));
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}

/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$user = User::whereId($id)->firstOrFail();
// $user->hasAllRoles(Role::all());
return view('admin.show', compact('user'));
}

public function assignRole($id){
$user = User::whereId($id)->firstOrFail()->assignRole('Staff');
redirect('/dashboard');

}

/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}

/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$user = User::whereId($id)->firstOrFail();
$user->destroy();

redirect('/dashboard');
}
}

最佳答案

添加特征 use HasRoles;在您的用户模型中。
use Spatie\Permission\Traits\HasRoles; 导入在模型文件的顶部。

关于php - 无法为用户分配角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34089485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com