phplaravelexceptionmass-assignment

Unable to override Model in Laravel


In the process of learning Laravel and following along with the video tutorials. During episode 11 @ 18 minutes 30 seconds in here.

  1. The instructor creates a new file Model.php inside the app directory.
  2. The new Model.php now extends Eloquent
  3. Inside the new Model.php he adds protected $guarded = [];
  4. He then extends the Post.php to the new Model.php

This allows him to define all the guarded fields in the new Model.php when doing a mass submission in his form. However when I fo this I still get a mass assignment exception.

If I change the Model.php to Models.php (notice the extra 's') then it works. I'm wondering why I can't have another file name Model.php inside the app directory to extend to?

<?php

namespace App;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Model extends Eloquent
{
    protected $guarded = [];
}

Solution

  • You are going to have to use the App\Model; Instead of the use Illuminate\Database\Eloquent\Model;