How to Use Get Parameters in URL’s Using Laravel

Laravel applications require passing data from one location to another. We often need to pass parameters alongside these URLs. Here we will go through the process to read these Get Parameters from URLs. 

Method 1: Reading a Parameter from a Query String.

A URL can contain a single or multiple parameters with a specific value. For example https://scratchcoding.dev/posts?search=Laravel, here the parameter search has a value of ‘Laravel’. 

To read this value, we can use the $request object using the following syntax. 

$request->input(‘parameter’), or either access is as a property like, $request->parameter.

A URL may also have multiple parameters like https://scratchcoding.dev/posts?search=Laravel&page=5.

The $request object then reads both parameters and returns in the result variable. Consider the example below. 

Route::get(‘articles/search’, [ArticlesController::class], ‘searchArticles’]);  – routes/web.php

Controller class:

<?php 

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ArticleController extends Controller {

public function searchArticle(Request $request) {

$articles = [

“Routing in Laravel” => “Laravel”,

“Validation in Laravel” => “JavaScript”

];

echo “The article you have searched is from the category: ” .   $articles[$request->term] ?? “ I don’t know the category of this article.”

}
}

Now when in the browser you type https://scratchcoding.dev/articles/search?term=Laravel, you get the result.

“The article you have searched is from the category:  Laravel”

Method 2: Reading a Parameter from a Route.

Routes are defined in the routes/web.php or the routes/api.php files. Routes define how Laravel responds to URLs. 

Parameters in Laravel routes are added inside curly braces. The parameter value is then replaced by the curly braces at that location. Consider the example below. 

Route::get(‘articles/{parameter}’, function($parameter) {

dd(“The browser says: “ . $parameter);

});

So when we type the above URL ‘https://scratchcoding.dev/articles/hello’ in the browser with the parameter value,‘hello’, it gives the output: The browser says: hello.

Method 3: Passing Route Parameters to Controller.

Laravel routes hold minimal code, and most of the logic and code is inside the blade or Controller files. Routes pass requests to Controller class where the function details are found. 

The curly braces syntax is used as the first parameter to pass data from a route to the Controller. While in the second parameter we call the Controller and function name that will receive the parameters. 

Consider the example below. 

Route::get(‘articles/{search}’, [ArticleController::class, ‘searchArticle’]);

{search} is the dynamic parameter that takes in a changing value. 

The corresponding Controller code is as follows.

<?php

namespace App\Http\Controller;

use Illuminate\Http\Request;

class ArticleController extends Controller {

public function searchArticle($search) {

$articles = [

“Routing in Laravel” => “Laravel”,

“Validation in Laravel” => “JavaScript”

];

echo “The article you searched for belongs to the category: ” .   $articles[$request->term] ?? “ I don’t know the category of this article.”

}

}

In this case, you can use the URL instead of query parameters, as shown below.

https://scratchcoding.dev/articles/Laravel shows:

“The article you searched for belongs to the category: Laravel”

Method 4: Route Model Binding – Inject a Model as a Route Parameter

A route facade can call a controller with one or more parameters from the browser. The parameter is mostly the id of an item from the database record. Route Model Binding makes the record in the parameter id get automatically injected as an Eloquent Model the Controller can use. To enable this we can pass Controller functions in a route along with parameters as shown below. 

Route::get(‘/articles/{article}’, [ArticleController::class, ‘show’]); 

Route Model Binding enables easy retrieval and displaying of an article’s data with minimal code. Consider the Article Model below.  

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Article extends Model {

}

And the Controller class,

namespace App\Http\Controllers;

use App\Models\Article;

class ArticleController extends Controller {

public function show(Article $article) {

return view(‘articles.show’, [‘article’ => $article]);

}

}

<html>

<head>

    <meta charset="utf-8">

    <title>Example blog by scratchcoding.dev</title>

    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.9.3/css/bulma.min.css">

</head>

<body>

<section class="hero is-info">

    <div class="hero-body">

        <div class="container">

            <h1 class="title">

                An example blog.

            </h1>

            <h2 class="subtitle">

                by scratchcoding.dev

            </h2>

        </div>

    </div>

</section>

<section class="section">

    <div class="container">

        <div class="columns">

            <div class="column is-half">

                <h1 class="title">{{ $article->title }}</h1>

                <h2 class="subtitle">Published: {{ $article->created_at }}</h2>

                <div class="content">

                    {!! $article->content !!}

                </div>

            </div>

        </div>

    </div>

</section>

<footer class="footer">

    <div class="content is-flex-align-items-flex-end mt-auto">

        <div class="container">

            <p>

                Made by scratchcoding.dev

            </p>

        </div>

    </div>

</footer>

</body>

</html>

This tutorial covers many powerful tools by Laravel to handle URL-based parameters in web applications. The different methods explained above, help retrieve parameters from query strings, curly braced parts of a URL, passing parameters directly to a Controller, and automatically adding a model instance based on a route parameter. These methods help easily create flexible and dynamic web applications. 

Leave a Comment