How to access cookie from view in PHP Laravel 5

Viewed 15097

I can access cookie in controller then pass it to view

//HomeController.php
public function index(Request $request)
{
  $name = Cookie::get('name');
  return view('index', ['name'=> $name]);
}

But I want to write a small control (widget) that can fetch data from cookie without concern of parent controller. For example, header, footer widgets could fetch its own data without main page controller knowing which data is needed.

I can query the data from database by using View Composer. But, how can I access data from view in the request cookie ?

4 Answers

You can use {{ Cookie::get('laravel_session') }} to print out the cookie inside your view.

Related