ItSolutionStuff.com

Laravel Convert Array to Query String Example

By Hardik Savani • December 7, 2023
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: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Remove Null and Empty Values from Laravel Collection?

Read Now →

Laravel Collection Remove Last Item Example

Read Now →

Laravel Collection Remove First Item Example

Read Now →

How to Get Random Item from Laravel Collection?

Read Now →

How to Convert Collection to Array in Laravel?

Read Now →

Laravel Collection map() Add Attribute Example

Read Now →

How to Get Request Method in Laravel?

Read Now →

Laravel Collection SortBy Tutorial with Examples

Read Now →

Laravel Collection Unique | Remove Duplicates from Collection Laravel

Read Now →

Laravel Collection Filter Method Example

Read Now →

How to Check Request is Ajax or Not in Laravel?

Read Now →

How to Check Request Method is GET or POST in Laravel?

Read Now →