Laravel user post image upload error - image

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])
);

Related

Call to a member function getClientOriginalExtension() on null laravel crud?

The name of controller is "BadgeController". This is my store function in BadgeController where is this error happening.
public function store(Request $request)
{
$request->validate([
'image' => 'required',
]);
$image = $request->file('images');
$new_name = rand().'.'.$image->getClientOriginalExtension();
$image->move(public_path('images'), $new_name);
$form_data = array(
'image' => $new_name,
);
Badge::create($form_data);
return redirect('badges.index')->with('success', 'Data Added successfully.');
}
Judging by the error, it means your validation is successfully passed, so I guess the input's name is image not images.
$image = $request->file('image');

How to upload image into database using 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();

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 Uploading a photo

Hi i am trying to upload a photo but i do not get the filename of the photo in my database..The photo successfully uploads to my folder.i have this in my controller
public function postCreatePost(Request $request)
{
if ($request->hasFile('avatar'))
$avatar = $request->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
Image::make($avatar)->resize(300,300)->save( public_path('uploads/posts/' .$filename));
Auth::user()->posts()->create([
'body' => $request->body,
'username' => Auth::user()->name,
'photo' => $filename,
]);
$posts = Post::orderBy('created_at', 'desc')->get();
return view('home', ['post' => $posts]);
}
Can anyone please tell me what i am missing

Resources