Did you try the alternative stated there aswell?
$path = Storage::putFile('avatars', $request->file('avatar'));
Also, which version of laravel are you running? Stupid question maybe, but I think this particulair way of storing files is 5.3 only.
I'm following the Laravel 5.3 documentation for uploading files. And i saw this example:
if($validator->passes()){
$path = $request->file('avatar')->store('avatars');
}
But when i try to store it like this i get an error that Method store does not exist. https://laravel.com/docs/5.3/filesystem#file-uploads
Did you try the alternative stated there aswell?
$path = Storage::putFile('avatars', $request->file('avatar'));
Also, which version of laravel are you running? Stupid question maybe, but I think this particulair way of storing files is 5.3 only.
Try that
Form
<form method="POST" action="/settings/home" files="true" class="form-horizontal">
{ csrf_field() }}
In controller head
use Intervention\Image\Image;
$imagename = $kind->path.'-'.$producer->path.'-'.Slug::make(Input::get('title')); //Your name
\Image::make(Input::file('preview'))->encode('jpg', 75)->save(base_path() . '/public/files/products/preview/' . $imagename .'.jpg');
$imageSaved = base_path() . '/public/files/products/preview/' . $imagename .'.jpg';
$product->product_preview = '/files/products/preview/' . $imagename .'.jpg';
I'm using Laravel 5.3, also there is a video in laracasts using this method. I've also tried the code 1:1 from the tutorial. But the same result.
https://laracasts.com/series/whats-new-in-laravel-5-3/episodes/12
Hmm, that's quite odd, maybe try posting both your view and controller here for further inspection?
This is the controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
use App\Http\Requests;
class UserController extends Controller {
protected $validationRules = [
'avatar' => 'mimes:jpg,png,gif,jpeg'
];
public function store(Request $request){
$validator = Validator::make($request->all(), $this->validationRules);
if($validator->passes()){
$path = $request->file('avatar')->store('avatars');
dd($path);
}else{
dd($validator->messages());
}
}
And the router
Route::post('/user/store', 'UserController@store');
Anyone ?
Please show us the entire form. Also, what is the exact error message?
make sure the <form> tag has enctype="multipart/form-data" otherwise there will not be a file object on which to call the store method.
@zachleigh I don't know how this will help you. But here is it.
BadMethodCallException in Macroable.php line 74:
Method store does not exist.
<form method='POST' action='/user/store' enctype="multipart/form-data">
<input type="file" name="avatar" />
{{ csrf_field() }}
<input type="submit" name="submit" value="submit" />
@Snapey This doesn't work at all. I've tried it with clean installation of laravel 5.3, i think that those methods are removed and the documentation is not updated!
What does this show? From your controller method:
dd($request->file('avatar'));
@xScence, Did you ever get this working? I'm having the same issue. request->file('blah'); says it's returning an Uploadedfile instance but the store and storeAs methods are not there.
@cotherman You have to increase you'r file upload limit in the php.ini For example:
upload_max_filesize = 500M
post_max_size = 500M
This should fix ur problem.
I'm facing the same issue. As a workaround you could use the move method:
$path = $request->file('avatar')->move(public_path().'/avatars', 'filename.extension');
I'm having the same issue. I've tried including this in my controller:
use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Storage;
...neither of those 2 seems to help. I still keep getting the same error as xScence was. The suggested answer only included a link to a webpage that showed us the method existed, but not how to access that method within our controller. Anyone know how I can call the store() method within my controller?
@jcgivens21 How big file u are trying to upload ?
@jcgivens21 you can only call store method if $request->file is an instance on UploadedFile object
dd($request->file) and check you are getting a file
@xScence I've tried varying sizes; however, for this example, let's just say 8 kb (a javascript file I chose at random from a temp directory).
@Snapey Thanks for the details. I get a 'null' return when I add dd($request->file); ....however, your suggestion made me think about it a little more closely, so I just did a basic dd($request); This showed me that there IS an uploaded file in the request, but it's being referred to as 'files' (plural, with the s on the end) instead of just 'file'.
So I then tried dd($request->files); and I get a result, not null:
FileBag {#45 ▼ #parameters: array:1 [▼ "project_document" => UploadedFile {#30 ▼ -test: false -originalName: "bootstrap-confirmation.js" -mimeType: "application/javascript" -size: 7455 -error: 0 path: "/tmp" filename: "phpruhnPw" basename: "phpruhnPw" pathname: "/tmp/phpruhnPw" extension: "" realPath: "/tmp/phpruhnPw" aTime: 2017-01-22 18:51:33 mTime: 2017-01-22 18:51:33 cTime: 2017-01-22 18:51:33 inode: 22054 size: 7455 perms: 0100600 owner: 1000 group: 0 type: "file" writable: true readable: true executable: false file: true dir: false link: false } ] }
So then if I simplify my controller function and just use this:
/*-- Upload a Project Document --*/
public function upload(Request $request)
{
$file = request()->file('project_document');
return dd($file);
}
I get this result:
UploadedFile {#314 ▼ -test: false -originalName: "bootstrap-confirmation.js" -mimeType: "application/javascript" -size: 7455 -error: 0 path: "/tmp" filename: "phpnOo2sZ" basename: "phpnOo2sZ" pathname: "/tmp/phpnOo2sZ" extension: "" realPath: "/tmp/phpnOo2sZ" aTime: 2017-01-22 18:53:03 mTime: 2017-01-22 18:53:03 cTime: 2017-01-22 18:53:03 inode: 22054 size: 7455 perms: 0100600 owner: 1000 group: 0 type: "file" writable: true readable: true executable: false file: true dir: false link: false }
Based on your response, this makes me believe that I DO have an UploadedFile object coming in with the post request.
So the next natural step for me is to update the function to store that file. Here is what the function looks like now:
/*-- Upload a Project Document --*/
public function upload(Request $request)
{
$file = request()->file('project_document');
$path = $file->store('project_documents/');
return dd($path);
}
That is when I get the error message:
BadMethodCallException in Macroable.php line 74: Method store does not exist.
So that is what I'm having trouble with, here. At the top of my controller, I have the following lines:
use Illuminate\Http\Request; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Storage; use App\Http\Requests;
Do I need to add another 'use' statement? I'm not even sure if the UploadedFile or Storage classes are necessary, but I thought I'd try adding them to get access to the 'store' method.
Maybe my syntax is wrong? All of the examples I've seen make it look really simple and straightforward, but I can't figure it out, lol. Like everything else, there's probably a brutally obvious answer to this that I'm missing.
@jcgivens21 it shouldn't matter, but to follow the docs
/*-- Upload a Project Document --*/
public function upload(Request $request)
{
$request->file('project_document')->store('project_documents/');
return ;
}
@Snapey Thank you for the prompt response. I attempted what you suggested and received the same error message.
@Snapey @xScence Hey fellas, thanks for your help. I wanted to let you (and anyone else who reads this) know that, despite THINKING I had upgraded to Laravel 5.3 on this app, it was actually at Laravel 5.2, so the store() function truly did not exist. Once I updated to 5.3 on that project, it resolved the file upload issue.
@jcgivens21 lol... it happens!
Please or to participate in this conversation.