Errorexception Array to string Conversion

Asked

Viewed 1,097 times

0

Hello, I’m starting a test project of my knowledge, I’m having problem in the variable line $response = $validate->$data; before I created the Rules was working normal, I tried help on the internet but it was unsuccessful, that’s why I’m here.

public function createClientStore(Request $request, CreateClientFormRequest $validate){

    $data = $request->except('_token');

    $nameClient = $data['nome'];
    $rand1 = rand(100000, 999999);

    $response = $validate->$data;

    if ($response['success'])
        return redirect()
                ->route('admin')
                ->with('success', 'Sucesso ao cadastrar!');

    else 
        return redirect()
            ->back()
            ->with('error', 'Precisa prencher os campos!');

    $dados = DB::table('clients')->insert($data);

    if ($request->hasFile('image') && $request->file('image')->isValid())
            $name = $rand1.kebab_case($nameClient);

    $extenstion = $request->image->extension();
    $nameFile = "{$nameClient}.{$extenstion}";
    $data["image"] = $nameFile;

    $upload = $request->image->storeAs('users', $nameFile);

    if (!$upload)
            return redirect()
                        ->back()
                        ->with('error', 'Falha ao carregar a imagem');

    if ($dados)
        return redirect()
                ->route('admin')
                ->with('success', 'Sucesso ao cadastrar');
    else
        return redirect()
                ->back()
                ->with('error', 'Erro ao cadastrar');

}

is giving error cited above, I will post also the rules()

public function rules()
{
    return [
        'nome'      => 'required|string|max:255',
        'nasc'      => 'required',
        'rg'        => 'required|max:255',
        'cpf'       => 'required',
        'genero'    => 'required',
        'logradouro'=> 'required',
        'endereco'  => 'required|max:255',
        'bairro'    => 'required|max:255',
        'cep'       => 'required',
        'email'     => 'required|max:255',
        'image'     => 'required',
    ];
}

Any help or tip I appreciate.

1 answer

0


Since, by your signature of the method createClientStore(), you are using Form Requests, your request is automatically validated by Laravel, before you even enter your method.

So your (wrong) call from $validate->$data is no longer required and you can consider that the input is valid.

If you need to customize errors, you must define them in the class CreateClientFormRequest. For more information, please consult the documentation: Form Request Validation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.