How can i upload a file and save the file using using original file name ?
Here my controller code ?
public function store(Request $request)
{
//Validate request data
$request->validate([
'name' => 'required',
'file' => 'required|mimes:pdf|max:50000',
'category' => 'required',
'year' => 'required|integer'
]);
$file = $request->file
$filename = Storage::disk('public')->putFile('regulations', $file);
Document::create([
'name' => $request->name,
'file' => $filename,
'category' => $request->category,
'year' => $request->year
]);
//redirect with success
return redirect()->back()->with('success', 'Done!');
}
Try this
$file = $request->file('file');
$file->getClientOriginalName();
Related
This issue appears after I submit a form and run form validation, even though my form validation is functioning on another page in that file.
Method Illuminate\Validation\Validator::validateDocs does not exist.
public function store(Request $request) {
$request->validate([
'title' => 'required',
'category' => 'required',
'content' => 'required|min:50',
'file' => 'required|docs|mimes:txt,pdf,ppt|max:2048'
$docsName = time() . '.'. $request->file->extension();
// $request->docs->move(public_path('docs'), $docsName);
$request->file->storeAs('public/docs', $docsName);
$postData = ['title' => $request->title, 'category' => $request->category, 'content' => $request->content, 'docs' => $docsName];
Post::create($postData);
return redirect('/post')->with(['message' => 'Post added successfully!', 'status' => 'success']);
}
I'm using the Spatie MediaLibrary library in a Laravel application. I want to upload 0 or more photos to my app via a REST API.
I can get it to work when the photo attribute contains 1 file
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'slug' => 'required',
'description' => 'required',
'price' => 'required|integer',
'photo' => 'nullable|file'
]);
$listing = Listing::Create([
'user_id' => auth('api')->user()->id,
'name' => $request->name,
'slug' => $request->slug,
'description' => $request->description,
'price' => $request->price,
]);
// stores the photo
if ($request->hasFile('photo')) {
$listing->addMediaFromRequest('photo')->toMediaCollection('photos');
}
return new ListingResource($listing);
}
The postman request looks as follows:
I know want to change the code so it can handle multiple photos in the request. I'm using the following code in the controller above to do so:
if ($request->hasFile('photo')) {
foreach ($request->input('photo', []) as $photo) {
$listing->addMediaFromRequest('photo')->toMediaCollection('photos');
}
}
and I have changed the attribute to photos[] instead of photo.
The code never goes into the foreach loop even.
Anyone has a hint on how to solve this?
Apparently the Spatie Medialibrary has a function called addMultipleMediaFromRequest. The full code is now
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'slug' => 'required',
'description' => 'required',
'price' => 'required|integer',
'photo' => 'nullable'
]);
$listing = Listing::Create([
'user_id' => auth('api')->user()->id,
'name' => $request->name,
'slug' => $request->slug,
'description' => $request->description,
'price' => $request->price,
]);
if ($request->hasFile('photo')) {
$fileAdders = $listing->addMultipleMediaFromRequest(['photo'])
->each(function ($fileAdder) {
$fileAdder->toMediaCollection('photos');
});
}
return new ListingResource($listing);
}
In Postman, I'm calling it as follows:
documentation reference
I managed to upload multiple files like this:
if($request->file('photos')) {
foreach ($request->file('photos') as $photo) {
$post->addMedia($photo)->toMediaCollection('post');
}
}
Check this out:
https://github.com/spatie/laravel-medialibrary/issues/227#issuecomment-220794240
This code is working for me.
View
<input type="file" name="photo[]" multiple />
ListingController
public function store(Request $request)
{
if ($request->hasFile('photo')) {
$fileAdders = $listing->addMultipleMediaFromRequest(['photo'])
->each(function ($fileAdder) {
$fileAdder->toMediaCollection('photos');
});
}
}
hi m trying to save data into db data saves but image isn't saving how to save it in db:
controller:
public function store(Request $request)
{
// dd($request->all());
$request->validate([
'category_name' => 'required',
'category_description' => 'required',
'category_slug' => 'required',
'category_image' => 'required|image',
]);
DB::table('categories')->insert([
'category_name' => $request->category_name,
'category_description' => $request->category_description,
'category_slug' => $request->category_slug,
'category_image' => $request->category_image,
]);
$path = $request->file('category_image');
$path->getClientOriginalName();
$path->move(public_path('images/backend_images/category_images');
return back();
}
Well... In your portion of code, there is no var.save(); so how will eloquent store any data without the given instruction?
Per example do something like this:
$userImage = new UserImage;
$UserImage->save();
public function store(Request $request)
{
// dd($request->all());
$request->validate([
'category_name' => 'required',
'category_description' => 'required',
'category_slug' => 'required',
'category_image' => 'required|image',
]);
$path = $request->file('category_image');
$nameImage = $path->getClientOriginalName();
$path->move(base_path('public/images/backend_images/category_images'), $nameImage);
DB::table('categories')->insert([
'category_name' => $request->category_name,
'category_description' => $request->category_description,
'category_slug' => $request->category_slug,
'category_image' => $nameImage,
]);
return redirect()->back();
}
code in my project and its work!
if ($request->hasFile('image')) {
$file = $request->file('image');
$filename = date('mdYHis') .str_random(5).'.'.$file->extension();
$request->image->move(base_path('public/images/rooms'), $filename);
$request->merge(array('image' => $filename));;
}
I think this would help. But you can browse for more answers.
Save to public directory
$path = 'images/backend_images/category_images';
$file = $request->file('category_image');
$filename = $file->getClientOriginalName();
$file->move(public_path($path), $filename);
And the, save to database
DB::table('categories')->insert([
'category_name' => $request->category_name,
'category_description' => $request->category_description,
'category_slug' => $request->category_slug,
'category_image' => $filename
]);
Hi im trying to upload image into database when i do this all its gave error like this.
(1/1) BadMethodCallException
Method getClientOrignalName does not exist.
<form action="{{route('post.store')}}" method="post" enctype="multipart/form-data">**strong text**
public function store(Request $request)
{
$this->validate($request,[
'title' => 'required|max:255',
'content' => 'required',
'feature' => 'required|image',
'category_id' => 'required'
]);
// dd($request->all());
//exit;
$featured = $request->feature;
$featured_new_name=time().$featured->getClientOrignalName();
$featured->move('uploads/posts',$featured_new_name);
$post = Post::create([
'title'=>$request->title,
'content'=>$request->content,
'feature'=>'uploads/posts/'. $featured_new_name,
'category_id'=>$request->category_id
]);
Session::flash('success','Post Created Successfully.');
}
You should use file() method for retrieve file information from request. Try this code,
public function store(Request $request) {
$this->validate($request,[
'title' => 'required|max:255',
'content' => 'required',
'feature' => 'required|image',
'category_id' => 'required'
]);
// use file() method for retrive file data
$featured = $request->file('feature');
$featured_new_name = time() . $featured->getClientOrignalName();
$featured->move('uploads/posts', $featured_new_name);
$post = Post::create([
'title'=>$request->title,
'content'=>$request->content,
'feature'=>'uploads/posts/'. $featured_new_name,
'category_id'=>$request->category_id
]);
Session::flash('success','Post Created Successfully.');
}
What to here ?
how to assign the value of $filenameTosTor for my 'photo' which is a database column?
public function update(Request $request, User $user)
{
$filenameWithExt = $request->file('profile_pic')->getClientOriginalName();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
Get the Extention
$extention = $request->file('profile_pic')->getClientOriginalExtension();
$filenameToStore = $filename.'_'.time().'.'.$extention;
$path = $request->file('profile_pic')->storeAs('public/profile_image', $filenameToStore);
$userupdate = User::where('id', $user->id)->update
([
'name' => $request->input('name'),
$user->profile_pic =$filenameToStore;
'photo'=> $request->($filenameToStore);
'last_name' => $request->input('last_name'),
'phone_number' => $request->input('phone_number'),
'address' => $request->input('address'),
'facebook_link' => $request->input('facebook_link'),
'twittr_link' => $request->input('twittr_link'),
'youtube_link' => $request->input('youtube_link'),
'Biography' => $request->input('Biography'),
]);
$userupdate->save();
return redirect('user.index');
}
Just use this variable like this:
'photo' => $filenameToStore,