LaravelDaily / quickadmin

Quick adminpanel builder package for Laravel 5
MIT License
600 stars 193 forks source link

Multiple Photo Fields in a single CRUD #126

Closed AlexMilutinovic closed 6 years ago

AlexMilutinovic commented 7 years ago

I've created CRUD containing two Photo fields (cover and map). Problem is that only the first selected image is being uploaded. So, if I first select the cover image, then map image, only the cover image is being uploaded.

I noticed that both fields are filled with string values. In above-mentioned case, cover field gets correct value (e.g. 1511684065-filename.jpg) and map field gets something like /tmp/jVJthcgcF. If I upload just the map image, it gets uploaded successfully and the filename is correct.

How can I solve this problem?

PovilasKorop commented 7 years ago

@aleksandarmilutinovic I guess it's a bug inside of the package which is not easy to solve. We are currently not actively supporting the free package fixes and switched to online version at quickadminpanel.com - so that's the one I would recommend if you want quick fix. Otherwise - this issue will wait until someone on the team will find time, a few weeks at least.

AlexMilutinovic commented 7 years ago

@PovilasKorop Thanks for reply. I'll probably try to fix it and make a pull request. I suppose that pull request verification will be quicker that fixing the bug :)

AlexMilutinovic commented 7 years ago

@PovilasKorop Here is a fixed FileUploadTrait. I couldn't create Pull Request so, please, revise and update the trait so this issue can be fixed officially. Thanks in advance.

There was a bug in recreating the request (I've commented the changes). I didn't want to change the entire logic, but just to fix current issue.

Please let me know if there is anything wrong with the fix. Thanks!

<?php

namespace App\Http\Controllers\Traits;

use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;

trait FileUploadTrait
{

    /**
     * File upload trait used in controllers to upload files
     */
    public function saveFiles(Request $request)
    {
        if (!file_exists(public_path('uploads'))) {
            mkdir(public_path('uploads'), 0777);
            mkdir(public_path('uploads/thumb'), 0777);
        }
        $newRequest = null; // Variable to hold a new request created by above array merging
        foreach ($request->all() as $key => $value) {
            if ($request->hasFile($key)) {
                if ($request->has($key . '_w') && $request->has($key . '_h')) {
                    // Check file width
                    $filename = time() . '-' . $request->file($key)->getClientOriginalName();
                    $file     = $request->file($key);
                    $image    = Image::make($file);
                    Image::make($file)->resize(50, 50)->save(public_path('uploads/thumb') . '/' . $filename);
                    $width  = $image->width();
                    $height = $image->height();
                    if ($width > $request->{$key . '_w'} && $height > $request->{$key . '_h'}) {
                        $image->resize($request->{$key . '_w'}, $request->{$key . '_h'});
                    } elseif ($width > $request->{$key . '_w'}) {
                        $image->resize($request->{$key . '_w'}, null, function ($constraint) {
                            $constraint->aspectRatio();
                        });
                    } elseif ($height > $request->{$key . '_w'}) {
                        $image->resize(null, $request->{$key . '_h'}, function ($constraint) {
                            $constraint->aspectRatio();
                        });
                    }
                    $image->save(public_path('uploads') . '/' . $filename);
                    // Determine which request's data to use further
                    $requestDataToMerge = $newRequest == null ? $request->all() : $newRequest->all();
                    // Create new request without changing the original one (prevents removal of specific metadata which disables parsing of a second file)
                    $newRequest = new Request(array_merge($requestDataToMerge, [$key => $filename]));
                } else {
                    $filename = time() . '-' . $request->file($key)->getClientOriginalName();
                    $request->file($key)->move(public_path('uploads'), $filename);
                    // Determine which request's data to use further
                    $requestDataToMerge = $newRequest == null ? $request->all() : $newRequest->all();
                    // Create new request without changing the original one (prevents removal of specific metadata which disables parsing of a second file)
                    $newRequest = new Request(array_merge($requestDataToMerge, [$key => $filename]));
                }
            }
        }

        return $newRequest == null ? $request : $newRequest;
    }
}
PovilasKorop commented 7 years ago

@AlexMilutinovic thanks a lot, one of our team members will test and push the fix, probably next week.

prashantshrma commented 6 years ago

@AlexMilutinovic Thank you so much, I was facing the same issue. Thanks a lot. 👍