Search code examples
phplaravelvoyager

how to create a folder in public/storage/annonces/ Month2020 when inserting image using laravel?


I want to insert the image of Annonces, so I insert link in db annonces\August2020\annonces.jpeg like laravel voyager, but I want to give me month and year automatically, uppercase month for 1st alphabet and year in NUMBERS like laravel voyager.

AnnoncesController.php

public function store(Request $request)
    {
       $Annonce = new Annonce();
       if($request->hasFile('image'))
       {
        $image = $request->file('image');
        $image->storeAs('public\annonces\August2020','annonces'.".".$image->extension());
        $Annonce->image = 'annonces\August2020\annonces' .".".$image->extension(); 
        }
        $Annonce->save();
        return Redirect::to("annonces")
        ->withSuccess('Great! file has been successfully uploaded.');
    }

Solution

  • you can do something like this, take a look:

    $destinationPath = "/Uploads/" . $id . "/" . jdate()->getYear() . "/" . jdate()->getMonth() . "/" . jdate()->getDay();
    

    note: jdate is persian time helper, instead of that use carbon or whatever for agust2020 you can do something like:

    "/uploads/".jdate()->getMonth().jdate()->getYear()."/". jdate()->getDay()