shavindip
shavindip

Reputation: 622

Update records of database table : Laravel

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?

Upvotes: 0

Views: 1267

Answers (2)

thchp
thchp

Reputation: 2404

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();

Upvotes: 0

MONSTEEEER
MONSTEEEER

Reputation: 580

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();        
}

Upvotes: 1

Related Questions