Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Dynamic Fillable in Models

Got stuck in a issue with laravel 5.2.

Following is the error during eloquent create operation(post call),

Mass Assignment Exception in Model.php 453: column_name

Following are the prerequisites, which are to be taken into consideration:

  1. Fillables in model are filled in a dynamic manner by the following code:
    public function __construct() {
         $this->fillable(\Schema::getColumnListing($this->getTable()))
    }
    

Following are the methods which are debugged till now:

  1. Before insertion, in controller, $model::getillableField(), gives proper fillable array.

  2. In model.php line(450),

    if ($this->isFillable($key)) {
          $this->setAttribute($key, $value);
    }
    

    the above code returns the value as "false" and $model::getFillableField() has the column_name in the array list.

  3. Hardcoding $fillable variable with columns of table removes the error. Please Help, where i am going wrong and what is the solution for it?

Thanks in advance.

like image 812
Web Development Avatar asked Oct 18 '16 06:10

Web Development


People also ask

What is fillable in Laravel Model?

The fillable property is used inside the model. It takes care of defining which fields are to be considered when the user will insert or update data. Only the fields marked as fillable are used in the mass assignment. This is done to avoid mass assignment data attacks when the user sends data from the HTTP request.

What is guarded in Laravel?

The guarded attribute is the opposite of fillable attributes. In Laravel, fillable attributes are used to specify those fields which are to be mass assigned. Guarded attributes are used to specify those fields which are not mass assignable.

How to use Eloquent in Laravel?

In laravel eloquent, we do not need to specify the table name to be used for the Post model. The plural name of the class will be considered as the table name unless we specify the name of the table explicitly. For example, in the above code, the name of the class is Post that works on the table posts.

What is Eloquent in Laravel?

Eloquent is an object relational mapper (ORM) that is included by default within the Laravel framework. An ORM is software that facilitates handling database records by representing data as objects, working as a layer of abstraction on top of the database engine used to store an application's data.


3 Answers

What you are really trying to do is make ALL fields fillable.

The correct way to do this in Laravel is this:

protected $guarded = [];

This works in 5.2, even though the documentation for it is found in 5.3.

(relevant source code for 5.2)

(Documentation from 5.3):

If you would like to make all attributes mass assignable, you may define the $guarded property as an empty array:

By setting $guarded to an empty array, you are creating an empty black list, allowing all fields to be mass assignable.

Also, if this model is ever going to be constructed directly from user input, please do not do this. Laravel requires either $fillable or $guarded to be defined for a reason. Unless your model has fields that are literally 1:1 with a public form, then allowing all fields to be writable on mass assignment is a security vulnerability.

like image 159
AgmLauncher Avatar answered Oct 22 '22 21:10

AgmLauncher


Try this. Put the below code in your model,

public function __construct()
{
    $this->setFillable();
}
public function setFillable()
{
    $fields = \Schema::getColumnListing('table_name_here');

    $this->fillable[] = $fields;
}

This makes each and every column is fillable from that table.

like image 36
suguna Avatar answered Oct 22 '22 21:10

suguna


Create a trait that uses the database columns.

<?php

namespace App\Traits;

use Illuminate\Support\Facades\Schema;

trait ColumnFillable
{
    public function getFillable()
    {
        return Schema::getColumnListing($this->getTable());
    }
}

Now use this trait in your models.

<?php

namespace App;

use App\Traits\ColumnFillable;

class MyModel extends Model
{
    use ColumnFillable;

    ...

Now you'll never have to manually specify $fillable.

like image 7
stardust4891 Avatar answered Oct 22 '22 19:10

stardust4891