我試圖在兩個(gè)表(角色和能力)之間建立關(guān)系,但是當(dāng)我運(yùn)行該函數(shù)時(shí),allowTo()它說 is undefined。我嘗試清除緩存和配置,但沒有執(zhí)行任何操作。如果有人能提供幫助那就太好了,謝謝!型號(hào):<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Role extends Model{ protected $guarded = []; public function abilities(){ return $this->belongsToMany(Ability::class)->withTimestamps(); } public function allowTo($ability){ $this->abilities()->save($ability); }shell 上的 Tinker 環(huán)境:>>> $user = App\User::find(7);=> App\User {#3044 id: 7, name: "Rebeca Tejedor", email: "rebtej@gmal.com", email_verified_at: null, created_at: "2020-08-13 10:29:46", updated_at: "2020-08-13 10:29:46", }>>> $role = Role::firstOrCreate(['name'=> 'lender']);[!] Aliasing 'Role' to 'App\Role' for this Tinker session.=> App\Role {#3041 id: 1, name: "lender", created_at: "2020-08-13 10:41:15", updated_at: "2020-08-13 10:41:15", }>>> $ability = Ability::firstOrCreate(['name'=> 'edit_items']);[!] Aliasing 'Ability' to 'App\Ability' for this Tinker session.=> App\Ability {#3040 id: 1, name: "edit_items", created_at: "2020-08-13 10:43:34", updated_at: "2020-08-13 10:43:34", }>>> $role = allowTo($ability)錯(cuò)誤:PHP Fatal error: Call to undefined function allowTo() in Psy Shell code on line 1該模型修補(bǔ)匠環(huán)境
1 回答

慕妹3146593
TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超9個(gè)贊
您以這種方式使用您的關(guān)系:
$role = allowTo($ability);
雖然你應(yīng)該像這樣使用它:
$role->allowTo($ability);
并在你的方法中:
public function allowTo($ability) { $this->abilities()->attach($ability->id); }
- 1 回答
- 0 關(guān)注
- 127 瀏覽
添加回答
舉報(bào)
0/150
提交
取消