Aronaman's avatar

multiple image

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:)

0 likes
11 replies
Nakov's avatar

@aronaman don't you need to iterate over the files:

$files = $request->file('front_slider');

if($request->hasFile('front_slider'))
{
    foreach ($files as $file) {
        // do your logic here for each file
    }
}
Aronaman's avatar

@nakov

 $files = $request->file('front_slider');

if($request->hasFile('front_slider'))
{
    foreach ($files as $file) {
         if (config('settings.front_slider') != null) {
                $this->deleteOne(config('settings.front_slider'));
            }

             $front = $this->uploadOne($files, 'img');
            Setting::set('front_slider[]', $front);
    }
}

traits

 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
        );
    }

error Argument 1 passed to App\Http\Controllers\Admin\SettingController::uploadOne() must be an instance of Illuminate\Http\UploadedFile, array given,

Nakov's avatar

@aronaman it is because of this line my friend:

$front = $this->uploadOne($files, 'img');

change it to this:

$front = $this->uploadOne($file, 'img');

Note you were passing the array, it should be single image instead :)

Aronaman's avatar

@nakov thanks

now this error Call to undefined function App\Traits\str_random()

Aronaman's avatar

add 1 file working and try to add more than 1 file show error of 404 not found

Nakov's avatar

@aronaman please share more of the error that you get. You can debug the error if you use APP_DEBUG=true in your .env file. Also by reading the storage/logs/ current log file. It should be something like laravel.log or with the date in the file as well.

Aronaman's avatar

@nakov I store the MULTIPLE image to config using service provider (settingServiceProvider).

i want to fetch in the frontend

"homeController"

  public function index()
    {
 $config=Config::settings();
        $fronts=$config->get('front_slider'); 
....
return view('home', compact('orgFeature','typeOfOrg','cities','featureOrgs','fronts'));

}

what i see config is not model . how can i make work in config files

home blade.php

@foreach($fronts as $front)
                 <div class="site-blocks-cover" style="background-image: url('{{$front->config('settings.front_slider') }}');" data-aos="fade" data-stellar-background-ratio="0.5">
                 </div>
 @endforeach

Nakov's avatar

@aronaman I really don't understand what you are doing here..

Do you have

'front_slider' => [
    'image1' => 'path_to_image',
    'image2' => 'path_to_image2',
]   

something like this in a config file?

Aronaman's avatar

@nakov yes , [ 'key' => 'front_slider', 'value' => '', ], value store array .

<div class="slide-one-item home-slider owl-carousel">

  @foreach(config('app.settings') as $front)
                 <div class="site-blocks-cover" style="background-image: url('{{$front->config('settings.front_slider') }}');" data-aos="fade" data-stellar-background-ratio="0.5">
                 </div>
               @endforeach


    </div>
Nakov's avatar
Nakov
Best Answer
Level 73

@aronaman add as I've showed you above:

'front_slider' => [
    'image1' => 'path_to_image',
    'image2' => 'path_to_image2',
]   

then you can access it like this:

 @foreach(config('app.front_slider') as $imageName => $imagePath)
    <div class="site-blocks-cover" style="background-image: url('{{$imagePath }}');" data-aos="fade" data-stellar-background-ratio="0.5">
    </div>
@endforeach 

Please or to participate in this conversation.