0

I need to update the database table according to the edited data.

controller

public function update(Request $request)
{
    $subscriptionplan = SubscriptionPlan::find($request->id);
    $subscriptionplan->update($request->all());        
    return back();        
}

But nothing happens when I submit the form. When I use dd($request->all()); at the beginning of the function, it correctly shows the edited data as follows.

array:10 [▼
  "_method" => "patch"
  "_token" => "gOCL4dK6TfIgs75wV87RdHpFZkD7rBpaJBxJbLHF"
  "editname" => "SUP_EVA_001"
  "editdesc" => "des"
  "editprice" => "1000.050"
  "editlimit" => "1"
  "editperunit" => "20.000"
  "editexceedunit" => "30.000"
  "productid" => "1"
  "id" => "1"
]

But database has not been updated.

My table name is Table: subscription_plans and model is SubscriptionPlan

These are the table columns:

protected $fillable = [
        'name',
        'description',
        'price',
        'usage_limit',
        'charge_per_unit',
        'charge_per_unit_exceed',
        'is_limit_exceed_considered',
        'product_id'
    ];

Any idea on how to solve it or what I have done wrong?

3
  • Are any errors being pushed to you storage/log/laravel.log file? Commented Oct 18, 2018 at 8:13
  • There is no such file like that in my project Commented Oct 18, 2018 at 8:19
  • Are you sure, laravel logs errors there. Commented Oct 18, 2018 at 8:22

2 Answers 2

1

If your solution did not work, try the 1by1 like this.

public function update(Request $request)
{
    $subscriptionplan = SubscriptionPlan::find($request->id);
    $subscriptionplan->_method = $request->_method;
    $subscriptionplan->_token = $request->_token;
    $subscriptionplan->editname = $request->editname;
    $subscriptionplan->editdesc = $request->editdesc;
    $subscriptionplan->editprice = $request->editprice;
    $subscriptionplan->editlimit = $request->editlimit;
    $subscriptionplan->editperunit = $request->editperunit;
    $subscriptionplan->editexceedunit = $request->editexceedunit;
    $subscriptionplan->productid = $request->productid;
    $subscriptionplan->save();        
    return back();        
}
Sign up to request clarification or add additional context in comments.

Comments

0

In order for Laravel to automatically fill the model attributes, the indexes of the array passed to the fill method must correspond to your model attributes names.

Also, instead of

$subscriptionplan->update($request->all());

Use

$subscriptionplan->fill($request->all());

Then save the subscription plan with $subscriptionplan->save();

7 Comments

Still database is not updating.
The attributes may be guarded, add attributes that need to change into a fillable array attributes. (doc about this: laravel.com/docs/5.7/eloquent#mass-assignment)
the model use "protected $fillable" method
The array indexes in $request->all() array should be the same as the model attributes. editname should be name, and so on
I think I have got it wrong there. I updated my question with the table columns.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.