laravel update() function not working Class
protected $fillable = [
'mobile_number',
'method',
'tracking_number',
'amount',
'user_id',
'total',
'approved'
];
Controller
public function updateSavings(Request $request) {
$request->validate([
'mobile_number' => 'required',
'method' => 'required',
'tracking_number' => 'required',
'amount' => 'required',
]);
$savings = new SavingAcount();
$savings->mobile_number = $request->mobile_number;
$savings->method = $request->method;
$savings->tracking_number = $request->tracking_number;
$savings->amount = $request->amount;
$savings->total = $request->total;
$savings->user_id = $request->id;
$savings->approved = 1;
$savings->update();
return redirect('/admin/savings')->with('status', 'savings Updated');
}
what does not working mean?
public function updateSavings(Request $request) {
dd($request->all());
}
everything shows up here? amount, method and so on? you may have validation errors, and you are redirected back
Why are you newing up an instance and then calling update on it; it doesn't exist. You need to save, not update if the rest of the logic is correct, meaning you intend to create a new record in the database.
when i save this. it makes new record. But I want to update
But you do nothing to retrieve the record from that database that you want to update. How do you pass the id of the SavingAccount record to the updateSavings method?
In that case you should type hint a property on the method that matches the wildcard on the route definition, e.g.
public function updateSavings(Request $request, SavingAccount $savingAccount) {
//...
No new needed
I solved that. But still not updating
Can you show the full controller method as it is now?
Even cut the validation code
public function updateSavings(Request $request, SavingAcount $savings) {
$savings->mobile_number = $request->mobile_number;
$savings->method = $request->method;
$savings->tracking_number = $request->tracking_number;
$savings->amount = $request->amount;
$savings->total = $request->total;
$savings->user_id = $request->id;
$savings->approved = 1;
$savings->update();
return redirect('/admin/savings')->with('status', 'savings Updated');
}
Use save not update; the update method takes an array of attributes
i know. But save method making new record instead of updating
In that case you have not understood earlier when I said that the typehinted argument must match the route wildcard . Show the route
Route::put('/admin/savings/update/{user_id}/{total}', 'AdminController@updateSavings');
You have a user_id and total as wildcards, that are unused in the controller action arguments?!! What are you doing???
Take a look at the docs for routing for goodness sake.
I am using it on the controller
You must to pass the information necessary to identify the SavingAccount model!!! Anything else is form data
$savings->mobile_number = $request->mobile_number;
$savings->method = $request->method;
$savings->tracking_number = $request->tracking_number;
$savings->amount = $request->amount;
$savings->total = $request->total;
$savings->user_id = $request->user_id;
$savings->approved = 1;
$savings->save();
on total and user_id. It comes from wildcard. this is not form data...
Is Laravel supposed to guess the correct SavingAccount? You said you were getting the SavingAccount model from routing, you are not. If the User has just one SavingAccount associated then get it through the relationship
Thank you so much. I fixed the issue..
Please sign in or create an account to participate in this conversation.