1

I'm trying upload image:

View (part):

 <input type="file" name="image" /> 

Countoller:

 public function store(Request $request){ dump($request->all()); $this->validate($request,[ 'title'=>'required|max:255', // 'image' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048', 'text'=>'required', ]); $imageName = time().'.'.$request->image->getClientOriginalExtension(); $request->image->move(public_path('images'), $imageName); dump($request); $data=$request->all(); dump($data); $aticle=new Article; $aticle->fill($data); } 

Dump request:

"title" => "fgdfd" "alias" => "dg" "desc" => "fdgfgd" "text" => "gd" "image" => "IMG_0002.JPG" "tag" => "hg" 

How do I put an image in MySql database?

0

3 Answers 3

1

As the docs describe, you should be using the file() method on the $request to access the uploaded file, not the name of your file field.

In your case, that means:

// Use the file() method to access the uploaded file $imageName = time() . '.' . $request->file('image')->getClientOriginalExtension(); // storeAs() allows you to move a file while specifying a new filename. // $path will be the fully qualified path to the file, including filename. $path = $request->file('image')->storeAs(public_path('images'), $imageName); 

It isn't clear from your question whether you want to save the file path in the database, or the actual binary file contents, as a BLOB. Here's how to do both:

// Create new article $aticle=new Article; $aticle->fill($data); // Either save the path to the uploaded image in the DB $aticle->featured_image = $path; $aticle->save(); // OR // Save the file binary contents in the DB $aticle->featured_image = file_get_contents($path); $aticle->save(); 
Sign up to request clarification or add additional context in comments.

Comments

0

Ideally, you'll save the file to a location and then store the path to that file in the database.

What version of Laravel are you on ? If you're on 5.3 or higher you can:

$path = $request->image->store('path/to/save/the/file'); 

this will save the file with a random name, then just store that path to the database.

or you can:

$path = $request->image->storeAs('path/to/save/the/file', 'filename.jpg'); 

if you want to specify the saved file name.

Comments

0

This work's for me:

Step 1

Use Intervention Image package for manipulating images. You'll install it via composer. Run the command:

composer require intervention/image 

Step 2

After installing Intervention Image, you need to add service providers and facade. Open our config/app.php file and add following lines. In the $providers array add the service providers for this package.

Intervention\Image\ImageServiceProvider::class 

Step 3

Add the facade of this package to the $aliases array.

'Image' => Intervention\Image\Facades\Image::class 

Step 4

And finally publish the configuration using following command:

$ php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravel5" 

Step 5 Controller

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use DB; use App\yourmodel; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Input; use Response; use Image; class YourController extends Controller { public function create() { return view('yourview.create'); } public function store(Request $request) { $file = Input::file('pic'); $img = Image::make($file); Response::make($img->encode('jpeg')); $var = new YourModel; $var->pic = $img; $var->save(); return redirect()->route('yourview.index') ->with('success','Success.'); } 

Step 6 View

<form action="{{ route('yourview.store') }}" method="POST" enctype="multipart/form-data"> @csrf <div class="row"> <div class="col-xs-4 col-sm-4 col-md-4"> <div class="form-group"> <strong>Image:</strong> <input type="file" id="pic" name="pic" class="form-control"></input> </div> </div> <div class="col-xs-12 col-sm-12 col-md-12 text-center"> <button type="submit" class="btn btn-primary">Send</button> </div> </div> </form> 

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.