呼叫未定義的方法 App\Models\User::attachRole() 錯誤
P粉418351692
P粉418351692 2023-08-30 14:08:49
0
1
475
<p><blockquote> <p>呼叫未定義的方法 App\Models\User::attachRole() 我是 Laravel 的絕對初學者 當我嘗試註冊時,我正在使用“laratrust”包,它給了我這個錯誤,我不知道為什麼</p> </blockquote> <pre class="brush:php;toolbar:false;">`<?php namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use App\Providers\RouteServiceProvider; use App\Models\User; use Illuminate\Foundation\Auth\RegistersUsers; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Validator; use Laratrust\Traits\HasRolesAndPermissions; class RegisterController extends Controller { use RegistersUsers; use HasRolesAndPermissions; protected $redirectTo = RouteServiceProvider::HOME; public function __construct() { $this->middleware('guest'); } protected function validator(array $data) { return Validator::make($data, [ 'name' => ['required', 'string', 'max:255'], 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], 'password' => ['required', 'string', 'min:8', 'confirmed'], ]); } protected function create(array $data) { $user = User::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => Hash::make($data['password']), ])/*->attachRole('user')*/; return $user->attachRole('admin'); } } `</pre></p>
P粉418351692
P粉418351692

全部回覆(1)
P粉738346380

您的 User 模型中沒有任何 atachRole 方法。 所以看來您錯過了安裝部分的第4步。

在您的使用者類別中新增 Laratrust\Contracts\LaratrustUser 介面和 Laratrust\Traits\HasRolesAndPermissions 特徵(通常位於 App\Models\User 中)。

use Laratrust\Contracts\LaratrustUser;
use Laratrust\Traits\HasRolesAndPermissions;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements LaratrustUser // 
{
    use HasRolesAndPermissions;  // <- This will add some methods and property to your class ( attachRole included )

    // ...
}
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!