Donny Gunawan
Donny Gunawan

Reputation: 363

Any Way to Auto Guarded Timestamps?

$fillable = ['*']

Model::create($request->all()); // frontend can inject 'created_at'

$guarded = ['created_at', 'updated_at', 'deleted_at']

Can we auto guard timestamp?


I don't want to do this:

$fillable = ['field_1........field_20']

Upvotes: 2

Views: 960

Answers (1)

Mauricio Rodrigues
Mauricio Rodrigues

Reputation: 807

You can use except() method of Illuminate\Http\Request that will return all request fields but will exclude of the list the specified keys.

// try this...
Model::create($request->except('created_at', 'updated_at', 'deleted_at'));

You can create a BaseRequest class that override the Illuminate\Http\Request, same as:

<?php

namespace YourClass\Name\Space;

class BaseRequest extends Illuminate\Http\Request
{
    const EXCEPT_FIELDS = ['created_at', 'updated_at', 'deleted_at'];

    public function all()
    {
        return $this->except(self::EXCEPT_FIELDS);
    }
}

and inject the new BaseRequest class into your controller instead of the Illuminate\Http\Request class.

Upvotes: 1

Related Questions