FareedR's avatar

How to use Auto Increment

I need to upload a file . I want to separate the file with auto increment id in database so that whenever user delete or user upload same filename , it wont conflict . Here the code .

public function store( BannerFormRequest $request)
    {
        if($request->hasFile('image'))
        {
            $filename = $request->image->getClientOriginalName();
            $request->image->move(public_path().'/banner/', $filename);  
        }

        $banner = new Banner ([
            'title'=> $request->get('title'),
            'image'=> $filename
        ]);

        $banner->save();
        alert()->success('Success Message','Banner has been added!')->autoClose(3000);
        return redirect('admin/list-banner');
    }

0 likes
14 replies
Vilfago's avatar

Autoincrement is handled automatically by your database, you just don't have to fill an id.

However, you need to set it to increment in your migration (or directly in your table).

public function store( BannerFormRequest $request)
    {
        if($request->hasFile('image'))
        {
            $filename = $request->image->getClientOriginalName();
        }

        $banner = new Banner ([
            'title'=> $request->get('title'),
            'image'=> $filename
        ]);

        $banner->save();
    $request->image->move(public_path().'/banner/'. $filename.'-'.$banner->id);  
        alert()->success('Success Message','Banner has been added!')->autoClose(3000);
        return redirect('admin/list-banner');
    }
s4muel's avatar
s4muel
Best Answer
Level 50

does it have to be the autoincrement id? if not, and a unique id is enough, then i suggest using the out-of-the-box store() method for file upload, here is the documentation: https://laravel.com/docs/5.7/filesystem#file-uploads

and here is an example:

$path = $request->file('avatar')->store('avatars');

public function store( BannerFormRequest $request)
    {
        if($request->hasFile('image'))
        {
            //the path will contain a uniquely generated ID which serves as a file name
            $path = $request->file('image')->store('banner'); //stores the file in the banners folder on your default public disk configured in config/filesystems.php

        }

        $banner = new Banner ([
            'title'=> $request->get('title'),
            'image'=> $path
        ]);

        $banner->save();
        alert()->success('Success Message','Banner has been added!')->autoClose(3000);
        return redirect('admin/list-banner');
    }


FareedR's avatar

@s4muel how about delete ? can i use this method ? if yes, it display error . if I didnt use your way , it works .

public function destroy($id)
    {
        $banner = Banner::find($id);
        unlink(public_path().'/banner/'.$banner->image);
        $banner->delete();
    }
s4muel's avatar

@FAREEDR - regarding the deleting, this should be enough:

Storage::delete($banner->image);
FareedR's avatar

@S4MUEL - @s4muel how to display it in blade file ? I always get 404 page . but the path is correct .

<div class="modal-body">
                                    <img src="/storage/{{ $banner->image}}">
                                </div>

FareedR's avatar

@s4muel I use every possible way to fix this but still 404 .

//Current code
<div class="modal-body">
       <img src="/storage/{{ $banner->image }}">
</div>

// Page Source
<img src="/storage/banner/DkfciHB1iqE1DfM1EdhGtcwjqQ5ZZVQAwCqyFxLa.png">

//filesystems.php
'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],
s4muel's avatar

check the storage, do you have that linked? quote from the documentation:

Remember, if you are using the local driver, all files that should be publicly accessible should be placed in the storage/app/public directory. Furthermore, you should create a symbolic link at public/storage which points to the storage/app/public directory.

FareedR's avatar

@s4muel php artisan storage:link is done

now my directory storage>app>banner

public>storage>gitignore

Vilfago's avatar

I was unable to do it (i.e the storage with symbolic link, even if the symbolic link was created) work on my hosting (shared one), it's the reason why I use the "ugly custom manual" way I suggested you.

But what @s4muel is lot better than mine, but it seems not to work on all configuration.

s4muel's avatar

@FAREEDR - weird, should be storage>app>public>banner instead of storage>app>banner

...but anyway, please try this:

    <img src="{{ asset("storage/$banner->image") }}">
FareedR's avatar

@S4MUEL - @s4muel i dont know where is the problem but here is current code

method 1 cant find the path but method 2 work perfectly

//filesystem.php
'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],

//GalleryController.php
public function store ( GalleryFormRequest $request)
    {
        if($request->hasFile('image'))
        {
        // method 1 leads to storage>app>galleries
            $pathImage = $request->file('image')->store('galleries');

        // method 2 leads to storage>app>public>galleries
        $pathImage = $request->file('image')->store('public/galleries');
        }

        $gallery = new Gallery ([
            'title'=> $request->get('title'),
            'description'=>$request->get('description'),
            'category'=>$request->get('category'),
            'image'=> $pathImage
        ]);

        $gallery->save();
        alert()->success('Success Message','Gallery has been added!')->autoClose(3000);
        return redirect('admin/list-gallery');
    }
1 like

Please or to participate in this conversation.