Laravel Convert Array to Query String Example

By Hardik Savani December 7, 2023 Category : Laravel

Hello Folks,

This post will give you an example of laravel array to query string. We will use how to convert array to query string in laravel. I would like to show you convert array to query string laravel. you can see laravel convert array to query string url.

In Laravel, you can convert array into query string using laravel helper. i will give you two simple examples to convert array to query parameters url in laravel. we will use Arr::query() and route() helpers to create query string from array.

So, let's see the simple examples:

Example 1:

Controller Code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Illuminate\Support\Arr;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index(Request $request)

{

$array = ["search" => "hardik", "sort_by" => "asc", "field" => "name"];

$queryString = Arr::query($array);

dd($queryString);

}

}

Output:

search=hardik&sort_by=asc&field=name

Example 2:

Controller Code:

>?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index(Request $request)

{

$array = ["search" => "hardik", "sort_by" => "asc", "field" => "name"];

$queryStringURL = route('users.index', $array);

dd($queryStringURL);

}

}

Output:

http://localhost:8000/users?search=hardik&sort_by=asc&field=name

I hope it can help you...

Tags :
Shares