Laravel Requests and Responses

Laravel requests and responses are the two ends of every page load. The Illuminate\Http\Request object gives you everything the browser sent, and response helpers make it easy to send back HTML, JSON, redirects or files.

Reading Input From a Laravel Request

Type-hint Request in a controller method and Laravel injects it. input() reads from both the query string and the body.

<?php
use Illuminate\Http\Request;

public function search(Request $request)
{
    $term = $request->input('q', '');
    $page = $request->integer('page', 1);
    $onlyActive = $request->boolean('active');
    $filters = $request->only(['category', 'sort']);

    return [
        'term' => $term,
        'page' => $page,
        'active' => $onlyActive,
        'filters' => $filters,
    ];
}
GET /search?q=php&page=2&active=1&sort=new
{"term":"php","page":2,"active":true,"filters":{"sort":"new"}}

Other Useful Request Methods

  • $request->has("email") / filled("email") check presence
  • $request->user() returns the logged-in user
  • $request->ip(), ->path(), ->url(), ->method() give request details
  • $request->header("Accept") reads a header
  • $request->expectsJson() tells you if the client wants JSON

Handling File Uploads

Uploaded files are UploadedFile objects. store() saves them with a random name on a configured disk.

<?php
public function avatar(Request $request)
{
    $request->validate(['avatar' => 'required|image|max:2048']);

    $path = $request->file('avatar')->store('avatars', 'public');
    $request->user()->update(['avatar_path' => $path]);

    return back()->with('status', 'Avatar updated');
}
Stored at storage/app/public/avatars/kD8s2aLq0x.png

Returning Responses

<?php
return view('dashboard', ['stats' => $stats]);          // HTML
return ['ok' => true];                                    // JSON automatically
return response()->json(['error' => 'Not allowed'], 403); // JSON with status
return redirect('/login');                                // redirect
return to_route('posts.index')->with('status', 'Saved');  // named route + flash
return back()->withInput();                               // previous page
return response()->download(storage_path('app/report.pdf'));
abort(404);                                               // error page
403 {"error":"Not allowed"}
302 Location: /posts  (flash "Saved" available once in the session)

Flash Messages in Views

Data added with ->with() is stored in the session for exactly one request. Show it in Blade with session().

@if (session('status'))
    <div class="alert">{{ session('status') }}</div>
@endif
<div class="alert">Saved</div>

Frequently asked questions

How do I get all input in Laravel?

Use $request->all(), but prefer $request->validated() or $request->only([...]) so you only work with fields you expect.

How do I return JSON in Laravel?

Return an array or Eloquent model and Laravel converts it to JSON, or use response()->json($data, $status) to control the status code and headers.

How do I redirect with a success message?

return redirect()->route("name")->with("status", "Message"); then read session("status") in the view.