Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel controller can't get cookie

I'm pretty new to laravel 5.6 and I want to use cookie inside a controller but I'm not able to retrieve cookie.

My purpose is to save some data inside cookie rather than get them on url because I don't want something like :

www.test.com/case/10156/folder/562/client/896/

but something more like

www.test.com/case/folder/client

So my function when I clikc on a link :

$(document).on('click', '.link', function(e){
    $.cookie("affaire", $(this).data("affaire"));
    $(location).attr('href',$(this).data("href"));
}); 

It works well, on chrome I can see the cookie "affaire". I'm redirect to /lots so my route is:

    Route::get('/lots', ['as' => 'lots', 'uses' => 'Copro\LotController@index']);

And my LotController@index :

    public function index()
    {
        $case_id = Cookie::get('affaire');
        dd($case_id);
    }

But it return "null" (I add "use Cookie" at the beginning of my controller) so I can't use this value to do something else.

I tried with Request :

public function index(Request $request)
{
    $copro_id = $request->cookie('affaire');
    dd($copro_id);
}

But it doesn't work either.

Someone know how to retrieve cookie ?

Thank for your help.

like image 351
Furya Avatar asked Jun 22 '18 11:06

Furya


1 Answers

Ok I finally found a solution to my problem :

https://pineco.de/accessing-front-end-cookies-laravel/

Cookies are encrypt with laravel so we must enter cookie name inside except list of the middleware.

like image 196
Furya Avatar answered Oct 20 '22 00:10

Furya