1

I am trying to upload an image into the database but unfortunately it is not inserting an image into the database. How can I fix it?

Database table: https://ibb.co/3sT7C2N

Controller

public function Add_slider(Request $request) { $this->validate($request, [ 'select_image' => 'required' ]); $content = new Sliders; if($request->file('select_image')) { $content->slider_image = Storage::disk('')->putFile('slider', $request->select_image); } $check = Sliders::create( $request->only(['slider_image' => $content]) ); return back() ->with('success', 'Image Uploaded Successfully') ->with('path', $check); } 
4
  • Are you trying to upload the file itself or just the filename? Commented Feb 25, 2020 at 12:09
  • I am trying to upload image file into database Commented Feb 25, 2020 at 12:14
  • @Zubair Where is your code to save image in the database? Commented Feb 25, 2020 at 12:15
  • You're storing the file in the Storage you want to store the image in the database as well? Or You want to store the file in the Storage and keep the file path in the database? Commented Feb 25, 2020 at 12:17

2 Answers 2

1

You should do with the following way:

public function Add_slider(Request $request) { $this->validate($request, [ 'select_image' => 'required' ]); $image = $request->file('select_image'); $extension = $image->getClientOriginalExtension(); Storage::disk('public')->put($image->getFilename().'.'.$extension, File::get($image)); $content = new Sliders; if($request->file('select_image')) { $content->slider_image = $image->getFilename().'.'.$extension;; $content->save(); $check = Sliders::where('id', $content->id)->select('slider_image')->get(); return back()->with('success', 'Image Uploaded Successfully')->with('path',$check); } } 

And in view blade file:

<img src="{{url($path[0]->slider_image)}}" alt="{{$path[0]->slider_image}}"> 
Sign up to request clarification or add additional context in comments.

Comments

0

This returns only the filename:

Storage::disk('')->putFile('slider', $request->select_image); 

Use this instead:

Sliders::create([ 'slider_image' => $request->file('select_image')->get(), ]); 

Make sure the column type from database is binary/blob.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.