0
votes

I'm new to Laravel. I am using laravel 5.4 and trying to validate and update data in a model. Code looks like this:

Route

Route::resource ('contribution-structure', 'ContributionStructureController');

ContributionStructureController

public function update(Request $request, $id)
{
    //
     $data = $this->validate($request, [
        'employer_name' => 'required|min:3',
    ]);

    $plansubmission = PlanSubmission::find($id);
    $plansubmission->update($data);


}

The validation works but when I update I get an error saying:

Argument 1 passed to Illuminate\Database\Eloquent\Model::update() must be of the type array, null given, called in C:\xampp\htdocs\tapp\app\Http\Controllers\ContributionStructureController.php on line 84 and defined

2
Why are you using 5.4 instead of current 6.x? I know in older version of Laravel did not return anything (but I'm not sure when it was added), which is what I think is what's going on here. Update to latest and this issue should go away.jfadich
why not $plansubmission-> employer_name = $request ->only('employer_name') use like this??A.A Noman
@A.ANoman It would have to be $plansubmission->employer_name = $request ->get('employer_name'); $plansubmission->save(). As only() returns an array, and you need to be sure to save the model afterwards, which updated() does automatically.jfadich

2 Answers

2
votes

The validator doesn't return anything in versions earlier than Laravel 5.5. To get your code to work I would recommend updated to that latest version. Especially for new projects, you should always start with the latest stable version.

If you don't want to update you need to pull the data from the request after validating.

public function update(Request $request, $id)
{
    $this->validate($request, [
        'employer_name' => 'required|min:3',
    ]);

    $data = $request->only('employer_name');

    $plansubmission = PlanSubmission::find($id);
    $plansubmission->update($data);
}
0
votes

Today i told about the update method in php framework laravel there is an error message gives when we apply update method in laravel

when we build the update method to update the data of the database table we face some issues for example The controller does not fetch the id The Route problem which route is good PUT, get, Post, Patch and the laravel version problem

first the controller issue

public function Delivery_charges_update(Request $request, $id)
    {
       // return $request->all();
        $request->validate([
                'start-km' => 'required',
                'end-km' => 'required',
                'amount' => 'required',
                 ]);

        $data = Deliverycharges::find($id);
        $data->start_km = $request->get('start-km');
        $data->end_km = $request->get('end-km');
        $data->amount = $request->get('amount');
        $data->update();


        return redirect('/SuperAdmin/Delivery_charges');
    }

then second the route issue you can make route by post method because it is best Route::post('/Delivery_charges_update/{id}', 'SettingController@Delivery_charges_update');

then third the laravel version issue so the latest version of laravel is 5.5

and this all issues solution is valid for this laravel version