Laravel Response

In Laravel, a response is what sent back to the user’s browser when a request made. All routes and controllers are meant to return some response as per the request. In Laravel, a response can be return in various ways.

Basic Response

In Laravel, simplest response is just to return a string from a controller or route.

Example:-

Attaching Headers

Headers can also be attached to a response using “header” or “withHeaders” method.

Syntax:-

Example:-

Attaching Cookies

Cookies  can also be attached to response using “cookie” helper method.

Syntax:-

Example:-

JSON Response

If you want to return a JSON response it can be done using “json” method.

View Response

If you want to return a view in response with custom status and headers, to do this “view ” helper method can be used in following way –

Force File Download

If you want to generate a force download response it can be done using “download” method.