Reputation: 290
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
Upvotes: 0
Views: 1431
Reputation: 144
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
Upvotes: 0
Reputation: 6348
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);
}
Upvotes: 2