Laravel Convert Array to Query String Example
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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Remove Null and Empty Values from Laravel Collection?
- Laravel Collection Remove Last Item Example
- Laravel Collection Remove First Item Example
- How to Get Random Item from Laravel Collection?
- How to Convert Collection to Array in Laravel?
- Laravel Collection map() Add Attribute Example
- How to Get Request Method in Laravel?
- Laravel Collection SortBy Tutorial with Examples
- Laravel Collection Unique | Remove Duplicates from Collection Laravel
- Laravel Collection Filter Method Example
- How to Check Request is Ajax or Not in Laravel?
- How to Check Request Method is GET or POST in Laravel?