4
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

Karl Hill
  • 12,937
  • 5
  • 58
  • 95

1 Answers1

4

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

Chris
  • 54,599
  • 30
  • 149
  • 186
  • I don't understand Chris, please write the correct code for me? Please – Edenilson Conceição Jan 28 '19 at 00:44
  • I have, it is right there above? You had `->arquivo` but it should be `->file('arquivo')` – Chris Jan 28 '19 at 00:45
  • Cris your code error: Call to a member function storeAs() on null – Edenilson Conceição Jan 28 '19 at 00:47
  • Not my code mate. The above error though would suggest that `arquivo` doesn't contain any uploaded data, are you sure you are uploading a file? Trying dumping out the request data with something like `dump($request->all())` – Chris Jan 28 '19 at 00:50
  • "_token" => "AltCjFjNN3FfZU8xhdjSE9M5ki6bxnAnmStZhDW8" "arquivo" => "download.png" "placa" => "w" "marca" => "w" "modelo" => "e" "cor" => "f" "tipcombust" => "r" "nportas" => "4" "quilometragem" => "22" "renavam" => "wqdqw" "chassi" => "wqd" "valordiaria" => "222" ] – Edenilson Conceição Jan 28 '19 at 00:58
  • after error again: "Call to a member function storeAs() on null" – Edenilson Conceição Jan 28 '19 at 00:58
  • $request->file('arquivo'); IS NULL. The method file() , its necessary some library???? – Edenilson Conceição Jan 28 '19 at 01:20
  • Relax. People are here trying to help in their own spare time... `file` is not part of an external library (it is part of laravel, please take 30mins to read through the docs I linked above). It seems your request isn't treating the file as a file, are you sure the form is set to multipart? Please update your question with your full controller method code and also the full form you are creating – Chris Jan 28 '19 at 01:23
  • Ok, its work, i created a new view with a new form e and its all right. – Edenilson Conceição Jan 28 '19 at 02:13
  • Are All Right!! Thanks, but my english is not very well – Edenilson Conceição Jan 28 '19 at 02:14