Crud: Default value for time in Controller

Created on 26 Sep 2016  路  9Comments  路  Source: Laravel-Backpack/CRUD

I want to set default value for insert date item in Conteroller like this

public function store(StoreRequest $request)
{
$request->merge(['date'=>now()]);
// Or
$this->dare=now();
return parent::storeCrud();
}
and didnt work
how to do that?

Most helpful comment

@tabacitu the reason I suggested the observer was because i think @A-Ronagh said that there was no UI / field for this date field.

So if you added a field with a default, that would then create a field element would it not? I guess you could make it a hidden field to prevent it from showing on the UI.

So if you want to make sure the date field is not visible on the form, you can either make it a hidden field, and use the default param, or you could use the observer method? (does that make sense? )

All 9 comments

Hi @A-Ronagh ,

I wonder - would a default value on the "date" field make more sense? That way, it's also obvious to the user how that date ended up there?

$this->crud->addField([   // Date
    'name' => 'birthday',
    'label' => 'Birthday',
    'type' => 'date',
    'default' => '2015-05-05'
]);

Cheers!

P.S. I'll go ahead and close this issue so we don't clog the section. Feel free to open it again if you encounter problems or comment if you have follow-ups.

I want to set value for model field befor save and dont have any ui to user set that I want to set this value for save in db how can i achive this ????
public function store(StoreRequest $request)
{
//how to set value for filed to save in db ??????
return parent::storeCrud();
}

You can set up a Model Observer.

What this will do is, whenever the model is saved/created it can run code.

If you follow the instructions here https://laravel.com/docs/4.2/eloquent#model-observers

You could do something like

    public function created(User $user){
        $user->date = date('Y-m-d H:i:s');
        $user->save();
    }

So when we a new model is created, it will insert the time into the database?

@A-Ronagh although I agree with @OwenMelbz 's solution, for such a small task I'd use

1) The 'default' attribute. Most fields have it. So if you set something like below, the default value will come pre-populated in the field.

$this->crud->addField([ // Text
    'name' => 'title',
    'label' => "Title",
    'type' => 'text',
    // optional
    'default' => 'some value'
]);

2) Eloquent Mutators on your model. This is more general - this function will be run every time you change this attribute in the database using Eloquent, wether you do it from the CRUD panel or any other place (your front-end, for ex).

    public function setFirstNameAttribute($value)
    {
        $this->attributes['first_name'] = strtolower($value);
    }

3) Observers, like @OwenMelbz suggested. They're an even more general way to accomplish this. This is closer to an event-driven architecture: "before _article_ is created _do this_".

Hope this helps you or future searchers. Cheers!

@tabacitu the reason I suggested the observer was because i think @A-Ronagh said that there was no UI / field for this date field.

So if you added a field with a default, that would then create a field element would it not? I guess you could make it a hidden field to prevent it from showing on the UI.

So if you want to make sure the date field is not visible on the form, you can either make it a hidden field, and use the default param, or you could use the observer method? (does that make sense? )

I done it like this

public function store(StoreRequest $request)
{
 $this->request->request->set('myfield', $mydefaultvalue);

  // your additional operations before save here
  $redirect_location = parent::storeCrud();
  // your additional operations after save here
  // use $this->data['entry'] or $this->crud->entry
  return $redirect_location;
}

protected function store(StoreRequest $request){
$vebol=$this->request->request->set('custom_path', 'MyDefaultValue');// I want to get the default value + input value from the user

    // your additional operations before save here
    // $redirect_location = parent::storeCrud();
    // your additional operations after save here
    // use $this->data['entry'] or $this->crud->entry
    // return $redirect_location;
    print_r($vebol);

when I print it I don't see the result. Could anyone can help me?

@sokvebolkot method set returns null or nothing. Do like this.
image

replace my request() to your $this->request

and also the controller method should simply use public instead of protected.

Was this page helpful?
0 / 5 - 0 ratings