我得到BadMethodCallException调用未定义的方法App \ Models \ User :: identifiableAttribute

问题描述

在应用程序的前端单击“新建帖子”按钮后出现此错误
帖子视图

enter image description here

我的日志文件中的一行:
[2020-09-27 14:41:03]本地。错误调用未定义方法App \ Models \ User :: identifiableAttribute(){“ exception”:“ [object](BadMethodCallException(code:0):在C:\ xampp \ htdocs \ backpack-demo \ vendor \ laravel \ framework \ src \ Illuminate \ Support \ Traits \ ForwardsCalls.PHP:50)中调用未定义的方法App \ Models \ User :: identifiableAttribute()

我正在使用Laravel 7 + Backpack CRDU生成

帖子控制器:

<?PHP

namespace App\Http\Controllers;

use App\Events\NewPost;
use App\Http\Requests\PostStoreRequest;
use App\Jobs\Syncmedia;
use App\Mail\ReviewPost;
use App\Models\Post;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class PostController extends Controller
{
    /**
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        $posts = Post::all();

        return view('post.index',compact('posts'));
    }

    /**
     * @param \App\Http\Requests\PostStoreRequest $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function store(PostStoreRequest $request)
    {
        $post = Post::create($request->validated());

        Mail::to($post->author->email)->send(new ReviewPost($post));

        Syncmedia::dispatch($post);

        event(new NewPost($post));

        $request->session()->flash('post.title',$post->title);

        return redirect()->route('post.index');
    }
}

帖子模型:

 class Post extends Model
    {
        use CrudTrait;
        /**
         * The attributes that are mass assignable.
         *
         * @var array
         */
        protected $fillable = [
            'title','content','published_at','author_id',];
    
        /**
         * The attributes that should be cast to native types.
         *
         * @var array
         */
        protected $casts = [
            'id' => 'integer','author_id' => 'integer',];
    
        /**
         * The attributes that should be mutated to dates.
         *
         * @var array
         */
        protected $dates = [
            'published_at',];
    
        public static function create(array $validated)
        {
        }
    
    
        public function author()
        {
            return $this->belongsTo(User::class);
        }
    }

用户模型:

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name','email','password',];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password','remember_token',];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',];
}

解决方法

您忘记了在用户模型中使用“ CrudTrait”:

use Backpack\CRUD\app\Models\Traits\CrudTrait;

class User extends Authenticatable
{
  use Notifiable,CrudTrait
    .......
 }  

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...