yaeykay's avatar

Problem on setBirthdayAttribute

Hello how can I set the birthday of a user in laravel. The inputs are named year, month and day (Im using or Dropdown). On my User.php I have this code to set the birthday attribute before storing into the database.

User.php

  public function setBirthdayAttributes()
  {
    return $this->attributes['year', 'month', 'day'] = date('Y-m-d', Input::get('year') . '-' . Input::get('month') . '-' . Input::get('day'));
  }

and it is not working. Please help.

0 likes
11 replies
RayRutjes's avatar

Getting your values from the Input on your model is a very bad practice. You should pass the Input values as params

public function setBirthdayAttributes($year, $month, $day) 

Then again, to me the setBirthdayAttributes method name is a bit confusing, could be read has a attribute mutator but its not. Moreover this method is not really useful. Why don't you simply do this in your controller:

$user->birthday = date('Y-m-d', Input::get('year') . '-' . Input::get('month') . '-' . Input::get('day'));

You alo will need some validation on the date part, and maybe make use of Carbon is an option on your model by declaring the birthday attribute as a date:

class User extends Eloquent {
    protected $dates = ['birthday'];

    ...

}
yaeykay's avatar

Adding that code in my controller is messy don't you think? How about in the UserRepository? Is it nice when I place that code in my UserRepository?

yaeykay's avatar

And BTW you have any suggestion about other method name instead of setBirthdayAttribute? Hehehe

RayRutjes's avatar
Level 4

The only responsibility of your controller is transforming your user input into DTOs for example, and delegating. So no it is not messy to set your values directly from your controller. Referring to the Input from a UserRepository is also a bad practice, think about re-usability. If you have an attribute called birthday, then no need to create a method name, as Eloquent has its on magic and you can already call setBirthdayAttribute even if it doesn't exist on your model.

yaeykay's avatar

You think this is corret way?

in my UseController.php

public function store()
 {
  $this->user->birthday = $this->birthday();

  $this->user->save(Input::all());
 }

 public function birthday()
 {
  return date('Y-m-d', strtotime(Input::get('year') . '-' . Input::get('month') . '-' . Input::get('day')));
 }

and in my User.php

protected $dates = ['birthday'];
kaugesaar's avatar

Hmm.. I probably wouldn't have used a function for it. I would just go at like this, using Input::only and implode() instead.

$user->birthday = date('Y-m-d', strtotime(implode('-',  Input::only('year','month','day'))));
yaeykay's avatar

Why not? how about in my update method? I will paste again the long command like this?

public function update()
{
  $user->birthday = date('Y-m-d', strtotime(implode('-', Input::only('year','month','day'))));
}

confused...

yaeykay's avatar

@santiagogg I read that link and I tried this code

User.php

public function setBirthdayAttribute($year, $month, $day)
 {
  return $this->attributes['birthday'] = date('Y-m-d', strtotime(implode('-', Input::only($year, $month, $day))));
 }

and in my UserController.php

public function store()
 {
  $this->user->save(Input::all());
 }

in my database the birthday is 0000-00-00 not working why?

santiagogg's avatar

The function expect one parameter that's why it doesn't work.. It depends on how you want to do it. If you are saving year, month & day in the model, you can use this attributes in the accessories. You should only use Input Facade in the controller. If you are not saving year , month & day you should build the date in the controller. If you want to use mass assignment you can add the birthday attribute to the array of params.

Please or to participate in this conversation.