34
loading...
This website collects cookies to deliver better user experience
["title" => "ttl", "slug" => "slg", "excerpt" => "exrpt", "body" => "bdy"]
, in the create()
, update()
or fill()
methods.MassAssigmentException
error.Post::create(['title' => "my new blog post", 'slug' => "my-new-blog-post"]);
Illuminate\Database\Eloquent\MassAssignmentException with message 'Add [title] to fillable property to allow mass assignment on [App\Models\Post].'
$fillable
property, like this<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
protected $fillable = ["title"];
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
protected $guarded = ["id"]; // Guard only id and allow the rest to be Mass Assignable
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
protected $guarded = []; // Allow Mass Assignment for all field. !! Watch Out !!
// protected $guarded = ["id"]; // Guard id column and allow the rest
// protected $fillable = ["title", "slug", "excerpt", "body"];
}