laravel model find return null for existing id
I am facing this issue of model find for existing id that is returning null and showing below error.
Creating default object from empty value
This id exists in the database but it is still returning null.
$model = Shoppinglist::find($id);
$model->buy_it = 1;
$model->qty= $qty;
$model->save();
This problem starts when I change the columns qty type from into(10) to float because I want to save floating values in db column. I have spend hours on this issue and still unable to solve. I have check by providing static id value in-place of variable $id. I have check by converting $id to int.
Before using find($id)
, you should check the key in your table is primary key, otherwise, you can use where('id', $id)->get()
.
There are cases to consider in this issue.
My suggestion first is that explicitly declare the primary key in your model:
protected $primaryKey = 'shopping_list_id'; // or 'id' depending on your specified primary key