Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

concat in url in larvel

Concatenate parameters in URL in Laravel

In this tutorial, I will give you an example of “How to Concatenate parameters in URL in Laravel”, So you can easily apply it with your laravel 5, laravel 6, laravel 7, and laravel 8 application.

First, what we’re doing here, This is the example :

Concatenate parameters in URL in Laravel

Concatenate parameters in URL in Laravel

In this example, we will pass id on web.php, and we Concat user email, we can concatenate multiple parameters to the end of a route, and get these parameters in Controller using strpos() function in Laravel.

strrpos in php :

The strrpos() function finds the position of the last occurrence of a string inside another string.

Note: The strrpos() function is case-sensitive.

resources\views\listblade.php

concatenate to the end of a route

      <div class="container">
         <h3>Concat and pass parameters in url | Laravel</h3>
         <table class="table">
            <thead>
               <tr>
                  <th>S.no</th>
                  <th>Name</th>
                  <th>Email</th>
                  <th>Address</th>
                  <th>Action</th>
               </tr>
            </thead>
            <tbody>
             @foreach($data as $key => $data)
               <tr>
                  <td>{{ $key+1 }}</td>
                  <td>{{$data->name}}</td>
                  <td>{{$data->email}}</td>
                  <td>{{$data->address}}</td>
                  <td>
                  <a href="{{route('concat.view',$data->id . '-' .$data->name. '-' .$data->email)}}">View</a>
                  </td>
               </tr>
            </tbody>
            @endforeach
         </table>
      </div>
 

routes\web.php

#concat and pass parameter in url Example
Route::get('/concat-parameter-example','TestController@concatparameter');
Route::get('/concat-parameter-example-view/{id}','TestController@concatparameterview')->name('concat.view');

App\Http\Controllers \TestController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Model\TestModel;

class TestController extends Controller
{

  #Concat parameter in url Example
  public function concatparameter(){
    $data = TestModel::all();
    return view('list',compact('data'));
  }

  #Get parameters from url
  public function concatparameterview($id){
     $id = substr($id, strrpos( $id, '-'));
     $email = substr($id,1);
     dd($email);

  }
}
parameters concat in laravel

pass parameter in url laravel

Finally, In this tutorial, we learned, How to Concatenate parameters in URL in Laravel.

Also Read: How to add dynamic meta tags in Laravel 8.