Home > Software design >  Pass date in url in Laravel
Pass date in url in Laravel

Time:03-22

I have this Link in my view

 <a href="{{route('detail', ['id'=>$row->id, 'date'=>Carbon\Carbon::now()->format('m-d-Y') ])}}">
 Link
 </a>

when i click on this link this url is called

detail?id=2&date=03-22-2022

getting this error :

Could not parse '03-22-2022': DateTime::__construct(): Failed to parse time string (03-22-2022) at position 0 (0): Unexpected character

but if my url is

detail?id=1&date=03/22/2022

then it is working how can i urlencode in laravel

Controller:

public function Detail(Request $request)
    {
         
        $start_date=date('Y-m-d H:i:s', strtotime($request->date));
    }

Thanks

CodePudding user response:

You can use urlencode function.

   <a href="{{route('detail', ['id' => $row->id, 'date' => urlencode(Carbon\Carbon::now()->format('m-d-Y')) ])}}">
     Link
   </a>

CodePudding user response:

In controller method use Carbon::createFromFormat and import carbon like below

use Carbon\Carbon;

then in method

public function Detail(Request $request)
{
         
       $startDate=Carbon::createFromFormat('m-d-Y',$request->date);

       dd($startDate->toDateTimeString());
}

CodePudding user response:

You can pass timestamp.

<a href="{{route('detail', ['id'=>$row->id, 'date'=> \Carbon\Carbon::now()->timestamp ])}}">
    Link
</a>

Controller

public function Detail(Request $request)
{
    $start_date=date('Y-m-d H:i:s', \Carbon\Carbon::now()->timestamp);
}
  • Related