I get BadMethodCallException Call to undefined method App\Models\User::identifiableAttribute()
Asked Answered
C

1

5

I get this error after clicking 'New Post' button the frontend of the app:
Posts view
enter image description here

Line from my log file:
[2020-09-27 14:41:03] local.ERROR: Call to undefined method App\Models\User::identifiableAttribute() {"exception":"[object] (BadMethodCallException(code: 0): Call to undefined method App\Models\User::identifiableAttribute() at C:\xampp\htdocs\backpack-demo\vendor\laravel\framework\src\Illuminate\Support\Traits\ForwardsCalls.php:50)

I am using Laravel 7 + Backpack CRDU generator

Posts Controller:

<?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');
    }
}

Posts Model:

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

User model:

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',
    ];
}
Capitally answered 27/9, 2020 at 14:49 Comment(14)
welcome to SO @Daniel Ngandu , we can not help you unless if you share the code that cause that error, and the related codeHispidulous
Please post some code that you have tried, as @Hispidulous mentioned above, otherwise we cant help and your question will be closed for need details or clarityHeaney
Hey @Hispidulous , i am playing around with backpack, and trying out the crud operator. And i am trying to create a new post. And thats how i get that error. Let me edit my question with imagesCapitally
please share you Post,User Models and the PostCrudControllerHispidulous
i would have to guess you have not implemented a method that backpack needs for that model, so you should read the documentation and follow what ever it says to do .. might be a trait or extending a certain modelKingwood
@Kingwood so my Posts model uses the CrudTrait function. I have updated my question with more detsCapitally
@Hispidulous kindly look at the updated questionCapitally
And the error has nothing to do with your Post it has to do with User ... so what do you think you are missing on User that is different from Post?... since you already just said what it isKingwood
@Kingwood Not sure, but this is a suggestion the laravel page brings 'Did you mean App\Models\User::getArrayableAttributes() ?'Capitally
Not finding where backpack is making that call to the User model/objectCapitally
okay lets try this again .... why did you add the CrudTrait to Post, and why does Post work with backpack but User (which does not have the CrudTrait) not work with it? hint hint, wink winkKingwood
Thanks @lagbox, got it!Capitally
@Daniel Ngandu , I hope your next question as same detailed as this oneHispidulous
Hehehe definitely, still learning the ropes.Capitally
H
16

your have forgotten to use 'CrudTrait' in your User Model:

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

class User extends Authenticatable
{
  use Notifiable,CrudTrait
    .......
 }  
Hispidulous answered 27/9, 2020 at 15:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.