Laravel update request

I am trying to update a user based on email, but not there, is there a way to do this without raw requests.

Currently error

{"error": {"type": "ErrorException", "message": "Create a default object from empty Value", "file": "C: \ WAMP \ WWW \ Celeb-jim \ App \ Controllers \ SignupController. php "," line ": 189}}

My code

public function changeAccountStatus ($plan, $userEmail ){ $UpdateDetails = User::where('email', '=', $userEmail)->first(); $UpdateDetails->member_type = $plan; $UpdateDetails->save(); } 
+15
php laravel
source share
5 answers

This error assumes that User::where('email', '=', $userEmail)->first() returns null, and not a problem updating your model.

Make sure you actually have a User before trying to change its properties or use the firstOrFail() method.

 $UpdateDetails = User::where('email', $userEmail)->first(); if (is_null($UpdateDetails)) { return false; } 

or using the firstOrFail() method, there is no need to check if the user is empty because it throws an exception ( ModelNotFoundException ) when the model is not found, which you can catch using App::error() http://laravel.com/ docs / 4.2 / errors # handling-errors

 $UpdateDetails = User::where('email', $userEmail)->firstOrFail(); 
+9
source share

You can use the Laravel query constructor , but this is not the best way to do this .

Check the Wader answer below for Eloquent - this is better, because it allows you to check that there is actually a user that matches the email address and handle the error if it is not.

 DB::table('users') ->where('email', $userEmail) // find your user by their email ->limit(1) // optional - to ensure only one record is updated. ->update(array('member_type' => $plan)); // update the record in the DB. 

If you have several fields to update, you can simply add more values ​​to this array at the end.

+12
source share

Try to do it like this.

 User::where('email', $userEmail) ->update([ 'member_type' => $plan ]); 
+7
source share
 $update = \DB::table('student') ->where('id', $data['id']) ->limit(1) ->update( [ 'name' => $data['name'], 'address' => $data['address'], 'email' => $data['email'], 'contactno' => $data['contactno'] ]); 
+2
source share

It is very easy to do. The code is listed below:

  DB::table('user')->where('email', $userEmail)->update(array('member_type' => $plan)); 
+1
source share

All Articles