How To Convert Array to Query String in Laravel 10?

Reading Time: 4 minutes
138 Views

Converting an array to a query string is a common requirement in web development, especially when working with APIs or handling HTTP requests. In Laravel 10, the process of transforming an array into a query string allows developers to efficiently pass array data as URL parameters.

In this tutorial, we’ll see the comprehensive process of converting an array to a query string in Laravel 10. This functionality empowers developers to format array data into a query string, facilitating its inclusion in URLs or HTTP requests.

Read More: Laravel with Gupshup API Send WhatsApp Message

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Methods To Convert an Array To Query String

Here, we will see three different ways to convert an array to query string in laravel.

  • Using http_build_query(), a PHP function
  • Using Arr::query()
  • Using route(), a laravel helper function

Let’s see each process in a very detailed way.

Method #1: Use “http_build_query()”

You can use PHP function to convert an array of data in query string formats,

// Your array of data
$data = [
    'name' => 'John Doe',
    'age' => 30,
    'city' => 'Example City',
    // ... other key-value pairs
];

// Convert the array to a query string
$queryString = http_build_query($data);

// Output or use the generated query string
echo $queryString;

Output

name=John+Doe&age=30&city=Example+City

Read More: How To Install and Use jQuery in Laravel Example

Method #2: Use “Arr::query()”

You can use concept of Arr support class of laravel,

use Illuminate\Support\Arr;

// Your array of data
$data = [
    'name' => 'John Doe',
    'age' => 30,
    'city' => 'Example City',
    // ... other key-value pairs
];

// Convert the array to a query string using Laravel's Arr class
$queryString = Arr::query($data);

// Output or use the generated query string
echo $queryString;

Output

name=John%20Doe&age=30&city=Example%20City

Method #3: Use “route()”

You can use concept of route() helper function of laravel,

// Your array of data
$data = [
    'name' => 'John Doe',
    'age' => 30,
    'city' => 'Example City'
];

// Output or use the generated query string
echo route("home", $data);

Output

http://127.0.0.1:8000/home?name=John%20Doe&age=30&city=Example%20City

That’s it.

We hope this article helped you to learn about How To Convert Array to Query String in Laravel 10 Example Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more