简体   繁体   English

PHP 致命错误:调用未定义的 function Laravel 修补程序

[英]PHP Fatal error: Call to undefined function Laravel tinker

I'm trying to establish a relation between two tables (Roles and Abilities) but when I run the function allowTo() it says is undefined .我正在尝试在两个表(角色和能力)之间建立关系,但是当我运行 function allowTo()时,它说是undefined I tried to clear the cache and config but didn't do anything.我试图清除缓存和配置,但没有做任何事情。 If anyone could help that'd be great, thanks!如果有人能帮忙就太好了,谢谢!

Model : Model

<?php

namespace 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);
   }

Tinker Environment on shell : shell 上的修补程序环境

>>> $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)

Error:错误:

PHP Fatal error: Call to undefined function allowTo() in Psy Shell code on line 1

The model model

Tinker enviroment修补程序环境

you are using your relation in this way:您正在以这种方式使用您的关系:

$role = allowTo($ability);

while you should use it like:而你应该像这样使用它:

$role->allowTo($ability);

and in your method:并在您的方法中:

public function allowTo($ability) {
    $this->abilities()->attach($ability->id);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM