I am using Laravel 5.4. I made a function for two (insert and update). When id is not found it will hit insert otherwise it will hit the update method. Everything is working well but the update elequent query is not updating the table. The method is for angularjs http.post method
Controller function:
public function postUserJson(Request $req)
{
//return "check". $req->input('id');
if ($req->input('id')=='') {
$user = new User();
$user->UserName = $req->input('username');
$user->Password = $req->input('password');
$user->save();
return "inserted";
} else {
$check= User::find($req->input('id'));
// $check->ID = $req->input('id');
$check->UserName = $req->input('username');
$check->Password = $req->input('password');
$check->save();
return "updated".$check;
}
}
This ( return "updated".$check;
) code return in console:
updated{"ID":13,"UserName":"sadek","Password":"456"}
Previous username was sade. After editing i got the edited name with sadek after save() method. But when i refresh it show sade
How can i update a table ?
you are sending data in json formate. your query string is not same. see
"UserName" != username
Password != password
make sure about this.
Eloquent provides a method for handling all of the logic you have above, it is called updateOrCreate()
From the docs:
You may also come across situations where you want to update an existing model or create a new model if none exists. Laravel provides an updateOrCreate
method to do this in one step. Like the firstOrCreate
method, updateOrCreate
persists the model, so there's no need to call save()
$flight = App\Flight::updateOrCreate(
['departure' => 'Oakland', 'destination' => 'San Diego'],
['price' => 99]
);
为什么F共享我的密码???
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.