Search code examples
laravellaravel-routinglaravel-request

Error: Call to a member function storeAs() on string


public function cadastraAutomovelHomeAdd(Request $request)
{
    $file = $request->arquivo;
    $upload = $request->arquivo->storeAs('products', 'novonomeaffffff.jpg');
    exit();
}

Form

<form method='post' action='/cadastrar' enctype="multipart/form-data">
    {{ csrf_field() }}
    <div>
        <h5>Placa do Veículo:</h5>
    </div>
    <input type="file" class="form-control" name='arquivo' required placeholder="IMAGEM DO VEÍCULO">
    <input type='submit'/>
</form>

Route

Route::post('/cadastrar','Automovel@cadastraAutomovelHomeAdd');

I submit the form, and I get the following error.

Call to a member function storeAs() on string


Solution

  • You need to wrap it as a file to access the file methods, something like:

    $upload = $request->file('arquivo')->storeAs('products', 'novonomeaffffff.jpg');

    https://laravel.com/docs/master/filesystem#storing-files