Search code examples
mysqllaravelsaveobservers

Laravel Eloquent is not saving properties to database ( possibly mysql )


I'm having a strange issue.

I created a model observer for my user model. The model observer is being run at 'saving'. when I dump the object at the very end of the user model to be displayed ( this is just before it saves.. according to laravel docs ) it displays all the attributes set correctly for the object, I've even seen an error that showed the correct attributes as set and being inserted into my database table. However, after the save has been completed and I query the database, two of the fields are not saved into the table.

There is no code written by myself sitting between the point where I dumped the attributes to check that they had been set and the save operation to the database. so I have no idea what could be causing this to happen. All the names are set correctly, and like I said, the attributes show as being inserted into the database, they just never end up being saved, I receive no error messages and only two out of ten attributes aren't being saved.

In my searches I have found many posts detailing that the $fillable property should be set, or issues relating to a problem with variables being misnamed or unset, however because I already have the specific attributes not being saved specified in the $fillable array, on top of the fact that they print out exactly as expected pre save, I don't believe those issues are related to the problem I am experiencing.

to save I'm calling:

User::create(Input::all());

and then the observer that handles the data looks like this:

class UserObserver {

   # a common key between the city and state tables, helps to identify correct city
   $statefp = State::where('id',$user->state_id)->pluck('statefp');

   # trailing zeros is a function that takes the first parameter and adds zeros to make sure 
   # that in this case for example, the dates will be two characters with a trailing zero, 
   # based on the number specified in the second parameter
   $user->birth_date = $user->year.'-'.$user->trailingZeros( $user->month, 2 ).'-'.$user->trailingZeros( $user->day, 2 );

   if(empty($user->city)){
      $user->city_id = $user->defaultCity;
   }

   $user->city_id = City::where( 'statefp', $statefp )->where('name', ucfirst($user->city_id))->pluck('id');

   # if the user input zip code is different then suggested zip code then find location data 
   # on the input zip code input by the user from the geocodes table
   if( $user->zip !== $user->defaultZip ){
      $latlon = Geocode::where('zip', $user->zip)->first();
      $user->latitude = $latlon['latitude'];
      $user->longitude = $latlon['longitude'];
   }

   unset($user->day);
   unset($user->month);
   unset($user->year);
   unset($user->defaultZip);
   unset($user->defaultCity);
}

that is the code for the two values that aren't being set, when I run

dd($user);

all the variables are set correctly, and show up in the mysql insert attempt screen with correct values, but they do not persist past that point.. it seems to me that possibly mysql is rejecting the values for the city_id and the birth_date. However, I cannot understand why, or whether it is a problem with Laravel or mysql.


Solution

  • since I was calling

    User::create();
    

    I figured I'd try to have my observer listen to:

    creating();
    

    I'm not sure why it only effected the date and city variables, but changing the function to listen at creating() instead of saving() seems to have solved my problem.