Falgun Lodaya
Falgun Lodaya

Reputation: 63

how to update data into table using in laravel

I want to update data into table if the record exits if not then create a new record.

if(!empty($request->meta_title)){
    $meta = new \stdclass();
    $meta = VendorMeta::firstOrNew(['vendor_id' => $id]);
    $meta->meta_title = $data['meta_title'];
    $meta->meta_desc = $data['meta_desc'];
    $meta->meta_keyword = $data['meta_keyword'];
    $meta->save();    
}

But I am getting this error:

MassAssignmentException in Model.php line 445:vendor_id

Upvotes: 1

Views: 116

Answers (2)

Thomas De Marez
Thomas De Marez

Reputation: 666

You need to set the $fillable property on your model.

Take a look at the docs under Mass Assignment.

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = ['meta_title', 'meta_desc', 'meta_keyword'];

Upvotes: 1

Amit Gupta
Amit Gupta

Reputation: 17688

You should define which model attributes you want to make mass assignable, so in your VendorMeta class add following code:

protected $fillable = ['vendor_id'];

Upvotes: 2

Related Questions