Aronaman
3 months ago
351
11
Laravel

multiple image

Posted 3 months ago by Aronaman

I created a setting in my web and create multiple image upload but show me an error.

creating traits for uploadAble

 public function uploadOne(UploadedFile $file, $folder = null, $disk = 'public', $filename = null)
    {
        $name = !is_null($filename) ? $filename : str_random(25);

        return $file->storeAs(
            $folder,
            $name . "." . $file->getClientOriginalExtension(),
            $disk
        );
    }

controller single upload work corretely, but the multile upload not working.

 use UploadAble;

public function update(Request $request)
{
    

    if ($request->has('site_logo') && ($request->file('site_logo') instanceof UploadedFile)) {

            if (config('settings.site_logo') != null) {
                $this->deleteOne(config('settings.site_logo'));
            }
            $logo = $this->uploadOne($request->file('site_logo'), 'img');
            Setting::set('site_logo', $logo);

        } elseif ($request->has('site_favicon') && ($request->file('site_favicon') instanceof UploadedFile)) {

            if (config('settings.site_favicon') != null) {
                $this->deleteOne(config('settings.site_favicon'));
            }
            $favicon = $this->uploadOne($request->file('site_favicon'), 'img');
            Setting::set('site_favicon', $favicon);

        }elseif ($request->has('front_slider[]') && ($request->file('front_slider[]') instanceof UploadedFile)) {

            if (config('settings.front_slider') != null) {
                $this->deleteOne(config('settings.front_slider'));
            }
            $front = $this->uploadOne($request->file('front_slider[]'), 'img');
            Setting::set('front_slider[]', $front);

        }
        else {

            $keys = $request->except('_token');

            foreach ($keys as $key => $value)
            {
                Setting::set($key, $value);
            }
        }
        return $this->responseRedirectBack('Settings updated successfully.', 'success');
}

on the blade

<div class="row mt-4">
                <div class="col-3">
                    @if (config('settings.front_slider[]') != null)
                        <img src="{{ asset('storage/'.config('settings.front_slider[]')) }}" id="frontImg" style="width: 80px; height: auto;">
                    @else
                        <img src="https://via.placeholder.com/80x80?text=Placeholder+Image" id="frontImg" style="width: 80px; height: auto;">
                    @endif
                </div>
                <div class="col-9">
                    <div class="form-group">
                        <label class="control-label">Front Slider</label>
                        <input class="form-control" type="file" name="front_slider[]" multiple onchange="loadFile(event,'frontImg')"/>
                    </div>
                </div>
            </div>

on the seeder

[
            'key'                       =>  'front_slider[]',
            'value'                     =>  '',
        ],

any help:)

Please sign in or create an account to participate in this conversation.