How to upload image into database using Laravel - laravel

I am trying to upload an image into the database. The image is not uploading properly, how can I fix it?
The image path is uploading into database like this: {"image":"phpV3IZnF.png"}
it should be like this: phpV3IZnF.png
Controller
public function servicesaction(Request $request)
{
$this->validate($request,[
'name' => 'required',
'description'=>'required',
'image' =>'required']);
$name=$request->get('name');
$description = $request->get('description');
$image = $request->file('image');
$extension = $image->getClientOriginalExtension();
Storage::disk('cms')->put($image->getFilename().'.'.$extension,
File::get($image));
$content = new Services;
$content->image = $image->getFilename().'.'.$extension;;
check = Services::where('id', $content->id)->select('image')-
>create(['service_name'=>$name,'description'=>
$description,'image'=>$content ])->get();
return back()->with('success', 'Success Successfully Inserted')-
>with('path', $check);
}

you can get file name like below
$fileName = md5($image->getClientOriginalName() . time()) . "." . $image->getClientOriginalExtension();
then save the name into database
Replace servicesaction function with below code
public function servicesaction(Request $request)
{
$this->validate($request,[
'name' => 'required',
'description'=>'required',
'image' =>'required']);
$name=$request->get('name');
$description = $request->get('description');
$image = $request->file('image');
$extension = $image->getClientOriginalExtension();
Storage::disk('cms')->put($image->getFilename().'.'.$extension,
File::get($image));
$content = new Services;
$content->image =$image->getClientOriginalName().'.'.$extension;;
check = Services::where('id', $content->id)->select('image')-
>create(['service_name'=>$name,'description'=>
$description,'image'=>$content ])->get();
return back()->with('success', 'Success Successfully Inserted')-
>with('path', $check);
}

change getFilename() to getClientOriginalName()
Like this
$extension = $image->getClientOriginalExtension()
$content->image = $image->getClientOriginalName().'.'.$extension;

Just Change 'image'=>$content to 'image'=>$content->image in the your create function
check = Services::where('id', $content->id)->select('image')
->create(['service_name'=>$name,'description'=> $description, 'image'=>$content->image ]) //use $content->image here
->get();

Related

How to upload multiple images in laravel so I can get them in frontend?

I want to do multiple image uploads in my laravel but so far i get errors like image must be image ... is chaos really ... help please???
public function store(Request $request) {
$request->validate([
'name' => 'required',
'image' => 'required|image|mimes:jpg,png,jpeg,gif,svg|max:2048',
'image.*' => 'image',
'description' => 'required',
'quantity' => 'required',
]);
$imgData = [];
if($request->hasfile('image')) {
foreach($request->file('image') as $file)
{
$path = '/images/phones';
$date = Carbon::now();
$tempName = env('APP_URL') . '/storage' . $path . '/'. $date->year.'/' . time() . '.' . $file->getClientOriginalName();
$filename = time() . '.' . $file->getClientOriginalName();
$filePath = $path . "/$date->year";
$file->storeAs( $filePath, $filename, 'public');
$imgData[] = $tempName;
}
$phones = new Phone;
$phones->name = $request->name;
$phones->description = $request->description;
$phones->quantity = $request->quantity;
$phones->price = $request->price;
$phones->brand_id =$request->brand_id;
$phones->image = json_encode($imgData);
$phones->save();
return redirect()->route('phones.index')
->with('success','Product has been created successfully.');
}
So far I get this error that image must be image and another error is displaying required extensions!!

Save image to database

I want to save image name to database but it always save to C:\xampp\tmp\phpAB3A.tmp. btw im not using xampp, im using laragon.How to change the path? i want to save to storage/app/public
'name'=> 'required',
'email' => 'required',
//'logo' => 'required',
$imageName = time().'.'.request()->logo->getClientOriginalExtension();
request()->logo->move(storage_path('app/public'), $imageName);
Company::create($request->all());
return redirect()->route('company.dashboard')->with('Success');
In your Company::create you have to define the path... Something like:Company::create([
'path' => torage_path('app/public',$imageName),....]);
I'm using laravel 5.8 and this code worked for me.
In controller
<?php
public function store()
{
// I did like this, because storeLogo method is reusable
$data = request()->validate([ /* ... */ ]);
$company = Company::create($data);
$this->storeLogo($company);
// but you can do like this
$data = request()->validate([ /* ... */ ]);
if (request()->has('logo')) {
$data['logo'] = request()->logo->store('', 'public');
}
$company = Company::create($data);
}
private function storeLogo(Company $company)
{
if (request()->has('logo')) {
$company->update([
'logo' => request()->logo->store('', 'public'),
// logo file is stored at /storage/app/public/
]);
}
}
$input = $request->all();
$fileName = '';
if ($request->hasFile('logo')) {
$destinationPath = storage_path('app/public');
$file = $request->logo;
$fileName = time() . '.'.$file->clientExtension();
$file->move($destinationPath, $fileName);
}
$input['logo'] = $fileName;
Company::create($input);

I have store the image file in admin through post method but when trying to put the same code for pdf/word its not working

I want to upload pdf file on page but its not working as done with image and its working
I have made a column filepath for pdf/word then code in postcontroller but not working
public function store(Request $request)
{
$this->validate($request, [
'title' =>'required',
'featured'=>'required|image',
'content'=>'required',
'category_id'=>'required'
]);
$featured= $request->featured;
$featured_new_name=time().$featured->getClientOriginalName();
$featured->move('uploads/posts', $featured_new_name);
$post = Post::create([
'title'=>$request->title,
'content'=>$request->content,
'featured'=>'uploads/posts/'. $featured_new_name,
'category_id'=>$request->category_id,
'slug'=>str_slug($request->title)
and when I trying to add "filepath column name in data base" for pdf/ word then using in public function store(Request $request)
{
$this->validate($request, [
'title' =>'required',
'featured'=>'required|image',
'content'=>'required',
'category_id'=>'required',
'file' => 'required',
]);
$featured= $request->featured;
$featured_new_name=time().$featured->getClientOriginalName();
$featured->move('uploads/posts', $featured_new_name);
$file=$request->file;
$file=time().$file->getClientOriginalName();
$extension = Input::file('file')->getClientOriginalExtension();
$filename = rand(11111111, 99999999). '.' . $extension;
$fullPath = $filename;
$request->file('file')->move(base_path() . '/uploads/pdf/', $filename);
$post = Post::create([
'title'=>$request->title,
'content'=>$request->content,
'featured'=>'uploads/posts/'. $featured_new_name,
'file'=>'uploads/pdf' .$filename,
'category_id'=>$request->category_id,
'slug'=>str_slug($request->title)
]);
Session::flash('success', 'New Blog has been Published on Website for Particular Menu');
return redirect()->back();
}
There is an issue in your code, you are saving the image in your base path whereas you want to save it into your public path so use
$request->file('file')->move(public_path() . '/uploads/pdf/', $filename);
instead of
$request->file('file')->move(base_path() . '/uploads/pdf/', $filename);
To save file update your code
validation of file type
'file' => 'required|mimes:pdf,doc|max:10000',
save file
if($request->hasfile('file'))
{
$file = $request->file;
$input = rand(11111111, 99999999).'.'.$file->getClientOriginalExtension();
$destinationPath = public_path('/uploads/pdf/100');
$file->move($destinationPath, $input);
}

Laravel user post image upload error

I have error while i am uploading Post Image I can save picture on my computer but in my database filename is distorted.
Here is my PostsController
public function store(Request $request, User $user, Image $image)
{
$this->validate($request, [
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'body' => 'required'
]);
if( $request->hasFile('image') ) {
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
Image::make($image)->save( public_path('uploads/images/' . $filename ) );
}
$image = $filename;
auth()->user()->publish(
new Post(request(['body', 'image'])));
return redirect('/');
}
The problem is that you try to insert the wrong variable into your database table. So, improve your code like this:
auth()->user()->publish(
new Post(['photo' => request('body'), 'image' => $image])
);

Upload image in public folder

I am trying to insert image in database using laravel 5.4. It works. But when I search that directory then it does not appear there. I also want to validate only images should be upload and size.
Thanks for advance.
My Controller function is here
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use Hash;
use App\User;
class regController extends Controller
{
public function create(Request $request)
{
$this-> validate(request(),[
'name' => 'required',
'email'=> 'required',
'type' => 'required',
'image' => 'required',
'password' => 'required|confirmed|min:6',
]);
$request['password'] = bcrypt($request -> password);
$User = new User($request->input()) ;
if($file = $request->hasFile('image')) {
$file = $request->file('image') ;
$fileName = $file->getClientOriginalName() ;
return Storages::putFile('public/images',$request->file('image'));
$destinationPath = public_path().'images/' ;
$file->move($destinationPath,$fileName);
$User->image = $fileName ;
}
$user =
User::create(request(['name','email','type','image','password']));
return redirect('login');
}
try this :
// Validation
$this->validate($request, [
'image' => 'required|image|mimes:jpeg,bmp,png|max:10240' // size in kelobytes this equals 10 mb
]);
$User = new User() ;
$User->password = bcrypt($request->password);
$User->name = $request->name;
$User->type = $request->type;
$User->email = $request->email;
$filename = $this->getFileName($request->image);
$request->image->move(base_path('public/images'), $filename);
$User->image = $fileName;
$$User->save();
return back()->with('success','Image Upload successful');
I just forgot to show you how to implement getFileName function to generate a unique filename sorry about that, in your controller you can define it as protected method like so:
protected function getFileName($file)
{
return str_random(32) . '.' . $file->extension();
}
I think you are tryin to do smth like this:
public function fileUpload(Request $request)
{
$this->validate($request, [
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$image = $request->file('image');
$input['imagename'] = time().'.'.$image->getClientOriginalExtension();
$destinationPath = public_path('/images');
$image->move($destinationPath, $input['imagename']);
$this->postImage->add($input);
return back()->with('success','Image Upload successful');
}

Resources