Normally when I call Model->save()
, it creates the new record in the database successfully. I'm trying to debug a situation when nothing happens and Model->save()
returns false. How do I find out what's happening?
$user = new User;
$user->fields = 'example';
$user->save(); // returns false
Running this does not show any insert queries.
dd(DB::getQueryLog());
But if I var_dump($user)
, I correctly get all the fields properly saved in the object.
Thanks!
Laravel Model->save() returns false but no error. Bookmark this question. Show activity on this post. Normally when I call Model->save() , it creates the new record in the database successfully.
Laravel Model::create or Model->save()$product = new Product(); $product->title = $request->title; $product->category = $request->category; $product->save();
Save can be used to both create a new Record and update a existing record . Whereas create is used to create a new record by providing all required field at one time .
Check if query got executed$newUser = User::create([ 'username' => Input::get('username'), 'email' => Input::get('email') ]); //Check if user was created if ( ! $newUser) { App::abort(500, 'Some Error'); } //User was created show OK message return Response::json(array('success' => true, 'user_created' => 1), 200);
To get the insert queries when $user->save();
error, you can try to catch the exception like this:
try{
$user = new User;
$user->fields = 'example';
$user->save(); // returns false
}
catch(\Exception $e){
// do task when error
echo $e->getMessage(); // insert query
}
Hope this helps :)
I know it's already answered, but in case someone else needs a different approach, I was able to see the $model->save() error with the underappreciated component of Laravel: tinker
php artisan tinker
$old = \App\Model\User::find(1);
$new = $old->replicate();
$new->save();
Then I was able to see the error:
Illuminate/Database/QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'relations' in 'field list' (SQL: insert into
temp_users
(...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With