I'm trying to validate some forms in Laravel, and when I call the function rules where I validate the form, it doesn't find the parameters: name, org., and image.
public function update($id)
{
$game = Game::find($id);
$game->name = $this->rules()->name;
$game->organization = $this->rules()->organization;
$game->image = $this->rules()->image;
$game->save();
return redirect('games');
}
public function rules()
{
return [
'name' => 'required',
'organization' => 'required',
'image' => 'required',
];
}
CodePudding user response:
I think the error message quite clearly explains what is wrong here.
You’re attempting to access the name rule returned from your rules function as though rules returns an object. Rules returns an array, therefore you need to access it as you would any other array element.
$this->rules()['name'];
The same applies for your other rules.
CodePudding user response:
I changed a little bit my code and used validate function so I can edit properly, this code worked for me to edit and not duplicate my Game object.
public function update(Request $request, $id)
{
$this->validate($request, [
'name' => 'required',
'organization' => 'required',
'image' => 'required',
]);
$game = Game::find($id);
$game->name = $request->name;
$game->organization = $request->organization;
$game->image = $request->image;
$game->save();
return redirect('games');
}