Hi,
Please, I need a bit of help with file uploading. This is what I am trying to achieve with my snippet of code of file uploading:
Saving the photo in a directory (seems problematic).
Saving the photo's link in the database (solved, but i welcome you suggestion if there is a better way).
Below is my code:
public function store(BlogRequest $request) {
$request = $request->all();
$request['picture'] = "/pictureStorage/" . $request['picture'];
Blog::create($request);
// The issue part
$name = $request->file('picture')->getClientOriginalName();
if($request->file('picture')->move('pictureStorage/ourFile', $name)){
return 'File was moved.'. 'you are now in'.URL::previous();
}else{
return "Something horrible went wrong!";
}
}
Actually, the error I am receiving is : "Call to a member function hasFile('picture') on array".
I understand that I may be calling the method the wrong way, so what is the correct way?
Thank you.
Consider using the Storage Facade for storing your files, this will ensure they land where you want them to and you can catch any unwanted exceptions
Also consider uploading the images in a separate call than creating your Blog this will allow you to validate the image before creating the Blog and when you finally create the blog, all you need to do is attach the id/path of the image.
Using same variable name (or array key) for multiple things is not a best practice, that's why I recommended you to change your column name and that one step will solve your issue.
@habeebnet ,
Yes, I got your point.
Actually, I am not a beginner in PHP programing, but Laravel. I am aware of the best practice, the snippet of code is just to try something out....like, the bigger picture is hidden.
Thank you for the input.