
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Pass an Array as a URL Parameter in Laravel
To pass an array as URL parameter you can make use of php built-in function http_build_query(). The http_build_query() returns you an URL-encoded query string.
Example 1
Using http_build_query()
Following is an example of this method ?
$data = array( 'field1' => 'test', 'field2' => 'xyz' ); echo http_build_query($data) . "
";
Output
The output of the above code is ?
field1=test&field2=xyz
The following example shows how to make use of http_build_query() when you have an array and the same needs to pass as a URL parameter.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; use Illuminate\Http\Response; class UserController extends Controller{ public function index(Request $request) { $yoururl = "https://www.test.com"; $params = array( "t1" => "abc", "t2" => "xyz" ); echo $yourfinalurl = $yoururl."?".http_build_query($params); } }
The output of the above code is ?
https://www.test.com?t1=abc&t2=xyz
Example 2
You can make use of serialize() and urlencode PHP built-in function to pass an array as URL param.
The serialize() function will return a sequence of bits for the input given and the urlencode will again encode the values as well the special characters available in it.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; use Illuminate\Http\Response; class UserController extends Controller{ public function index(Request $request) { $yoururl = "https://www.test.com"; $params = array( "t1" => "abc", "t2" => "xyz" ); $firstserialize = serialize($params); echo $yourfinalurl = $yoururl."?".urlencode($firstserialize); } }
Output
The output of the above code is ?
https://www.test.com?a%3A2%3A%7Bs%3A2%3A%22t1%22%3Bs%3A3%3A%22abc%22%3Bs%3A2%3A%22t2%22%3Bs%3A3%3A%22xyz%22%3B%7D
Example 3
Making use of http_build_query() and urlencode() functions.
urlencode() ? it will encode the values into a string.
http_build_query() ? returns you an URL-encoded query string.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; use Illuminate\Http\Response; class UserController extends Controller{ public function index(Request $request) { $yoururl = "https://www.test.com"; $params = array( "t1" => "abc", "t2" => "xyz" ); echo $yourfinalurl = $yoururl."?".urlencode(http_build_query($params)); } }
Output
The output of the above code is ?
https://www.test.com?t1%3Dabc%26t2%3Dxyz
Example 4
Using urlencode() and jsonencode()
Following is an example to pass an array as a URL parameter using jsconencode() and urlencode() functions ?
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; use Illuminate\Http\Response; class UserController extends Controller{ public function index(Request $request) { $yoururl = "https://www.test.com"; $params = array( "t1" => "abc", "t2" => "xyz" ); echo $yourfinalurl = $yoururl."?".urlencode(json_encode($params)); } }
Output
The output of the above code is ?
https://www.test.com?%7B%22t1%22%3A%22abc%22%2C%22t2%22%3A%22xyz%22%7D