Update an image in laravel - laravel

I want to update user image but it's not getting updated.
I tried with $request->hasFile('image') but when I return these statement it always returns false
here is my controller
if($request->hasFile('image'))
{
$file = $request->file('image');
$file->move(public_path().'/image/',$file->getClientOriginalName());
$file_name = $file->getClientOriginalName();
DB::table('settings')
->where('id', $id)
->update(['logo' => $file_name]);
}
when I write
return response()->json($request->hasFile('image'));
it will return always false

try use this one instead
if ($request->file('image')) {
// your image upload code here
}

Related

Image resizing not working with Intervention image Package

The photo is saved, but without resizing, the original photo is actually saved
protected function uploadImage($images = '') {
$path = 'upload/images';
if (request()->hasFile('image') && $files = request()->file('image'))
$images = Image::make($files->store($path, 'public_files'))->resize(320, 240);
return $images;
}
public function store(CreateGalleryRequest $request, Job $job) {
$image = $this->uploadImage();
if ($request->hasFile('image')) {
$data['image'] = $image . $image->dirname . '/' . $image->basename;
} else
$data['image'] = null;
$job->gallery()->create($data);
return redirect(route('jobs.gallery.index' , ['job' => $job->id]));
}
You need to call save() function to save image
...
$images = Image::make($files->store($path, 'public_files'))->resize(320, 240)->save('image path');
...
wrote:
$images = Image::make($files->store($path, 'public_files'))->resize(320, 240)->save($files->store($path, 'public_files'));
error:
Illuminate\Database\QueryException
actually there is an error in the following code:
$job->gallery()->create($data);

unlink image only if exists in laravel

While updating the user image unlink image from public folder if exists otherwise do update the user with image. Currently I have no image for user. And while updating user from profile section I am getting this error unlink('images/users') is a directory. I want if image exists for user then unlink the image and upload the new one otherwise just upload the new image.
My controller:
public function changeUserImage(Request $request)
{
$this->validate($request, [
'image' => 'required|mimes:jpeg,jpg,png|max:10000',
]);
$image = $request->file('image');
if (isset($image)) {
$imageName = time() . '.' . $request->image->getClientOriginalExtension();
if (!file_exists('images/users')) {
mkdir('images/users', 0777, true);
}
if (file_exists('images/users')){
unlink('images/users/' . \auth()->user()->image);
$image->move('images/users', $imageName);
User::find(\auth()->user()->id)->update(['image'=>$imageName]);
}else if (!file_exists('images/users')){
$image->move('images/users', $imageName);
User::find(\auth()->user()->id)->update(['image'=>$imageName]);
}
}
return redirect()->back();
}
Try this. I haven't test it yet. Let me know if you have any questions.
Make sure to Import File: use File;
UPDATED
public function changeUserImage(Request $request)
{
$this->validate($request, [
'image' => 'required|mimes:jpeg,jpg,png|max:10000',
]);
// Let get the current image
$user = Auth::user();
$currentImage = $user->image;
// Let compare the current Image with the new Image if are not the same
$image = $request->file('image');
// The Image is required which means it will be set, so we don't need to che isset($image)
if ($image != $currentImage) {
// To make our code cleaner let define a directory for DRY code
$filePath = public_path('images/users/');
$imageName = time() . '.' . $request->image->getClientOriginalExtension();
if (!File::isDirectory($filePath)){
File::makeDirectory($filePath, 0777, true, true);
}
$image->move($filePath, $imageName);
// After the Image has been updated then we can delete the old Image if exists
if (file_exists($filePath.$currentImage)){
#unlink($filePath.$currentImage);
}
} else {
$imageName = $currentImage;
}
// SAVE CHANGES TO THE DATA BASE
$user->image = $imageName;
$user->save();
return redirect()->back();
}
To store the image: $request->image->storeAs('images/users/', $file_name);
To delete an image: Storage::delete('images/users/'. $file_name);

want to generate pdf from laravel request data

i tried to generate pdf from my request data function. but it can't pass variable into my pdf generate function. i get requested data from this function.
public function fetch_data()
{
if(request()->ajax())
{
if(request()->from_date != '' && request()->to_date != '')
{
$data = DB::table('sales')
->whereBetween('dlvd_date', array(request()->from_date, request()->to_date))
->get();
} else {
$data = DB::table('sales')->orderBy('dlvd_date', 'desc')->get();
}
echo json_encode($data);
}
}
i want to get this $data value into my pdf generated function like below.
function convert_sales_data_to_html($data)
{
$sales_data = $data;
dd($sales_data);
$output = '';
return $output;
}
and pass to this function
public function pdf($data)
{
$pdf = \App::make('dompdf.wrapper');
$pdf->loadHTML($this->convert_sales_data_to_html($data))->setPaper('a4', 'landscape');
return $pdf->stream();
}
i tried this so many times but show me errors with missing argument. how can i fix this or have another method to generate pdf from requested data in laravel.
this is my routes
Route::post('salesreport/fetch', 'Report\SalesReportController#fetch_data')->name('salesreport.fetch_data');
Route::get('salesreport/pdf', 'Report\SalesReportController#pdf');

How to upload multiple images and store their name in database with laravel 5.1?

I have created a form for users can upload multiple images,and move uploaded images to 'Upload' folder and store their names in database. This is my code
public function multiple_upload() {
$multiupload = new Multiupload();
// getting all of the post data
$files = Input::file('images');
// Making counting of uploaded images
$file_count = count($files);
// start count how many uploaded
$uploadcount = 0;
foreach($files as $file) {
$rules = array('file' => 'required'); //'required|mimes:png,gif,jpeg,txt,pdf,doc'
$validator = Validator::make(array('file'=> $file), $rules);
if($validator->passes()){
$destinationPath = 'uploads';
$filename = $file->getClientOriginalName();
$upload_success = $file->move($destinationPath, $filename);
$uploadcount ++;
$multiupload->fileimage = $filename;
$multiupload->save();
}
}
if($uploadcount == $file_count){
Session::flash('success', 'Upload successfully');
return Redirect::to('/');
}
else {
return Redirect::to('/')->withInput()->withErrors($validator);
}
}
After upload all images successfully move to 'Uploads' folder but, in database it store only one image name. So how to store all images name in database?
Please help me and thanks you for help.
The reason is that you are reusing the same Multiupload instance in your loop and just overwriting the saved name with the name of next file. You should be creating a new Multiupload instance for every file that gets uploaded.
As #edrzej.kurylo said
You have to add the below line to inside of foreach($files as $file) {
$multiupload = new Multiupload();
Because you are reusing the same Multiupload function again and again. You have to re initialize the Model for every time the loop runs.
You should move your $multiupload = new Multiupload(); into the foreach loop.
foreach($files as $file) {
$multiupload = new Multiupload();
}
I would use for loop in this manner:
if($request->hasFile('images'){
$files = $request->file('images');
for($i=0; $i<count($files); $i++){
$img = new SampleImage();
$name = rand().'.'.$files[$i]->getClientOriginalExtension();
$files[$i]->move('uploads/samples/',$name);
$img->image_name = $name;
$img->save();
}
}

image upload code is not working

i am new in laravel, i am facing a big problem and i can't solve this problem.
I am trying to upload image into my database and upload folder,but my code is not working,
Here is my controller code
public function store()
{
$rules=array(
'gallery_name'=>'required',
'image_title'=>'required',
'image'=>'required'
);
$file = Input::file('image');
$destinationPath = 'uploads/';
// If the uploads fail due to file system, you can try doing public_path().'/uploads'
$filename = str_random(32) . '.' . $file->getClientOriginalExtension();
//$filename = $file->getClientOriginalName();
//$extension =$file->getClientOriginalExtension();
//get all book information
$galleryInfo = Input::all();
//validate book information with the rules
$validation=Validator::make($galleryInfo,$rules);
$upload_success = Input::file('image')->move($destinationPath, $filename);
if($validation->passes())
{
//save new book information in the database
//and redirect to index page
if ($upload_success) {
//save in the Band table database
Gallery::create($galleryInfo);
return Redirect::route('galleries.index')
->withInput()
->withErrors($validation)
->with('message', 'Successfully created Gallery.');
}
else {
return Redirect::route('galleries.create')
->withInput()
->withErrors($validation)
->with('message', 'Image fields are incomplete.');
}
}
//show error message
return Redirect::route('galleries.create')
->withInput()
->withErrors($validation)
->with('message', 'Some fields are incomplete.');
}
please help me as soon.
Thank you.

Resources