Uploading Images and storing in database
I have a profile image upload field along with the other fields of a user.
<div class="col">
<p><i class="text-muted fa fa-photo fa-fw mr-1"></i>Photo</p>
<label class="custom-file">
{{ Form::file('image', null, array('id' => 'image', 'class' => 'custom-file-control', 'placeholder' => '')) }}
<span class="custom-file-control"></span>
</label>
</div>
It works great but when it stores the item in the database it saves the image field as 'C://whatever.tmp'
I need it to store only the filename of the image and extension, as I'm saving it to the laravel app/storage/uploads folder.
Below is my controller, but I don't understand how to overwrite/modify $request->image so that when it does the storing in the database it will only use the filename instead of exactly the
response from the form.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Person;
use Intervention\Image\ImageServiceProvider;
use Illuminate\Support\Facades\Input;
class PersonController extends Controller
{
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
'image' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$file = $request->file('image');
$extension = $request->image->extension();
$path = $request->image->store('uploads');
$image = 'hi';
$request->image = 'hi';
Person::create($request->all());
return redirect()->route('people.index')
->with('success','Item created successfully');
}
}
All of my attempts to change the image are unsuccessful! What am I missing? EDIT: Formatting SUCKS
@laradonk this should do the trick
public function store(Request $request)
{
$this->validate($request, array(
'name' => 'required',
'image' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
));
//save the data to the database
$person = new person ;
$person->name = $request->name;
if($request->hasFile('image')){
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
Image::make($image)->resize(300, 300)->save( storage_path('/uploads/' . $filename ) );
$person->image = $filename;
$person->save();
};
$person->save();
return redirect()->route('people.index')
->with('success','Item created successfully');
}
make sure to use this in the top of your controller
use App\Person;
use Image;
the file will be moved inside the storage folder under the uploads folder .
i the field image of the form
<div class="form-group">
{!! form::file('image',['class'=>'form-control','placeholder'=>''])!!}
</div>
Also change
'files' => 'true' to
'files' => true
in the form tag
Please or to participate in this conversation.