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',
    ];
}
Community
  • 1
  • 1
Daniel Ngandu
  • 81
  • 1
  • 2
  • 11
  • 2
    welcome to SO @Daniel Ngandu , we can not help you unless if you share the code that cause that error, and the related code – OMR Sep 27 '20 at 14:51
  • 2
    Please post some code that you have tried, as @OMR mentioned above, otherwise we cant help and your question will be closed for _need details or clarity_ – STA Sep 27 '20 at 14:59
  • Hey @OMR , 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 images – Daniel Ngandu Sep 27 '20 at 15:11
  • 1
    please share you Post,User Models and the PostCrudController – OMR Sep 27 '20 at 15:12
  • 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 model – lagbox Sep 27 '20 at 15:14
  • @lagbox so my Posts model uses the CrudTrait function. I have updated my question with more dets – Daniel Ngandu Sep 27 '20 at 15:21
  • @OMR kindly look at the updated question – Daniel Ngandu Sep 27 '20 at 15:21
  • 1
    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 is – lagbox Sep 27 '20 at 15:22
  • @lagbox Not sure, but this is a suggestion the laravel page brings 'Did you mean App\Models\User::getArrayableAttributes() ?' – Daniel Ngandu Sep 27 '20 at 15:24
  • Not finding where backpack is making that call to the User model/object – Daniel Ngandu Sep 27 '20 at 15:25
  • 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 wink – lagbox Sep 27 '20 at 15:26
  • 1
    Thanks @lagbox, got it! – Daniel Ngandu Sep 27 '20 at 15:30
  • @Daniel Ngandu , I hope your next question as same detailed as this one – OMR Sep 27 '20 at 15:41
  • Hehehe definitely, still learning the ropes. – Daniel Ngandu Sep 27 '20 at 15:52

1 Answers1

15

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

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

class User extends Authenticatable
{
  use Notifiable,CrudTrait
    .......
 }  
OMR
  • 11,736
  • 5
  • 20
  • 35