Transforming Data with Laravel Collection Methods
In Laravel, a collection is an object that wraps an array of items and provides methods for changing or selecting those items.…
In a Laravel web application, a route connects an incoming request, such as a request for /posts/42, to the code that handles it. A named route gives the route a name, such as posts.show. Laravel’s route() helper uses the route name to generate the corresponding URL.
Use named routes for links, form actions, and redirects instead of repeating path strings throughout your code. If you later change a route’s path, Laravel generates the updated URL wherever your code uses its name. The Laravel named-routes overview demonstrates this pattern with route definitions and generated URLs.
Call name() on a route definition to give it a label. For example, in routes/web.php:
use App\Http\Controllers\PostController;
use Illuminate\Support\Facades\Route;
Route::get('/posts/{post}', [PostController::class, 'show'])
->name('posts.show');
/posts/{post} is the path pattern; {post} marks a value Laravel expects in the URL. The name posts.show is what other code uses to refer to this route. You could later change the path to /articles/{post} and keep the name, so links generated from posts.show follow the updated path.
Use distinct names for routes that serve different purposes. For example, a list page, a detail page, and an update endpoint can be named posts.index, posts.show, and posts.update. Route names must be unique so Laravel can identify the intended route.
route()Call route() with a route name to generate its URL. Pass the route’s parameter as a second argument when the route contains a placeholder:
$url = route('posts.show', ['post' => 42]);
// Example result: /posts/42
Laravel matches the post key to {post} in the route definition, then substitutes its value when building the URL. The route helper examples also show passing a route parameter by ID.
In a Blade template, use the generated URL for a link:
<a> $post->id]) }}">
View post
</a>
A hardcoded link such as /posts/{{ $post->id }} repeats the path pattern in the template. If the route path changes, you must find and edit that link separately. route() keeps the template tied to the route name instead.
Use the same helper for a form’s action. A search form that submits a GET request to the posts index can look like this:
<label for="q">Search posts</label>
<button type="submit">Search</button>
For an update form, Laravel supports method spoofing: the form sends a standard POST request, and a hidden _method field tells Laravel to handle it as a PUT request. @csrf adds a token that Laravel checks before accepting the submission. Use @method('PUT') to specify the intended method:
$post->id]) }}" method="POST">
@csrf
@method('PUT')
<label for="title">Title</label>
title }}">
<button type="submit">Save changes</button>
The route definition must accept PUT for this form to reach the update action:
Route::put('/posts/{post}', [PostController::class, 'update'])
->name('posts.update');
Laravel’s @method directive supplies the hidden field used to represent PUT or DELETE, which standard HTML forms do not send directly. The Laravel form example uses a named route in the form action with this directive.
When a route contains placeholders, pass values that match those placeholders. For /posts/{post}, route('posts.show', ['post' => $post->id]) supplies the post value. Named-route parameters also work with URLs that have more than one path segment; see the Laravel parameter examples.
After saving a post, a controller can redirect to its detail page by name:
return redirect()->route('posts.show', ['post' => $post->id]);
That redirect uses the same route definition as the Blade link. Changing the route path therefore updates both generated links and redirects, so the controller and template do not need to store the path themselves.
A route group can add the same starting text to several route names, called a shared name prefix. For example, routes inside a group declared with Route::name('admin.')->group(...) can use names such as admin.posts.index. Choose prefixes that keep names unique and make each route’s purpose clear.
Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.
Take a look at vroni.com