Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

netcore's avatar

Create data without having column in $fillable

Hello

How can I use Model::create() to insert data without having fillable column?

For example, table structure

data
    - name
    - level

In model (look, i have't added level in fillable array)

/* ... */
protected $fillable = [ 'name' ];
/* ... */

Controller

public function process (  Request $request ) {
    
    /*
    data given in request:
    'name' => 'something'
    */

    $data = $request->all();
    $data['level'] = 5,     
    Model::create ($data);
}

when create method is issued, level is not inserted, because i don't have added it to fillable

If i will add it to fillable, users will be able to inspect-element my frontend code to add level manally.

How to fix this?

0 likes
5 replies
igorblumberg's avatar
Level 7

You can do something like:

$model = new Model;
$model->name = "name";
$model->level = 5;
$model->save();
1 like
thomaskim's avatar

@netcore You can fill it and hide it by adding this to your model:

Edit: But $hidden will only hide it from your model's JSON form.

    protected $fillable = ['name', 'level'];
    protected $hidden = ['level'];
phildawson's avatar

@thomaskim hidden just hides it from its array/json representation?

@netcore

$model = new Model($request->all());
$model->level = 5;
$model->save();
public function process (Request $request, Model $model) {
    $model->fill($request->all());
    $model->level = 5;
    $model->save();
}
1 like

Please or to participate in this conversation.