Laravel Eloquent没有将属性保存到数据库(可能是mysql)

Laravel Eloquent没有将属性保存到数据库(可能是mysql),mysql,laravel,save,observers,Mysql,Laravel,Save,Observers,我有一个奇怪的问题 我为我的用户模型创建了一个模型观察者。模型观察者正在“保存”状态下运行。当我在要显示的用户模型的最末端转储对象时(就在它保存之前..根据laravel docs),它会显示为对象正确设置的所有属性,我甚至看到一个错误,该错误将正确的属性显示为设置并插入到我的数据库表中。但是,在完成保存并查询数据库后,其中两个字段不会保存到表中 在我转储属性以检查属性是否已设置以及将操作保存到数据库之间,没有我自己编写的代码。所以我不知道是什么导致了这一切。所有名称都设置正确,正如我所说,属性

我有一个奇怪的问题

我为我的用户模型创建了一个模型观察者。模型观察者正在“保存”状态下运行。当我在要显示的用户模型的最末端转储对象时(就在它保存之前..根据laravel docs),它会显示为对象正确设置的所有属性,我甚至看到一个错误,该错误将正确的属性显示为设置并插入到我的数据库表中。但是,在完成保存并查询数据库后,其中两个字段不会保存到表中

在我转储属性以检查属性是否已设置以及将操作保存到数据库之间,没有我自己编写的代码。所以我不知道是什么导致了这一切。所有名称都设置正确,正如我所说,属性显示为插入数据库,它们永远不会被保存,我没有收到错误消息,十个属性中只有两个没有被保存

在我的搜索中,我发现许多帖子详细说明了应该设置$fillable属性,或者与变量命名错误或未设置有关的问题,但是,因为我已经在$fillable数组中指定了未保存的特定属性,而且这些属性的打印结果与保存前的预期完全一致,我不相信这些问题与我所经历的问题有关

为了节省时间,我打电话:

User::create(Input::all());
然后处理数据的观察者如下所示:

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);
}
这是我运行时未设置的两个值的代码

dd($user);
所有变量都设置正确,并以正确的值显示在mysql插入尝试屏幕中,但它们不会持续超过该点。。在我看来,mysql可能拒绝了城市id和出生日期的值。但是,我不明白为什么,也不知道这是Laravel还是mysql的问题。

因为我打过电话

User::create();
我想我应该试着让我的观察者听:

creating();

我不知道为什么它只影响日期和城市变量,但将函数改为creating()而不是save()似乎解决了我的问题。

您需要显示填充用户对象和保存项目的代码