How are you creating the url? Are you using a helper class form Laravel? Should this URL point to a certain route?
Cheers
hello
how can i expand/add a new param to my url in a blade template?
example: http://example.com/ports?page=2 result: http://example.com/ports?page=2&tu=arg
How are you creating the url? Are you using a helper class form Laravel? Should this URL point to a certain route?
Cheers
If you mean the pagination part you can do this
$resource->appends(Request::only('value1', 'value2'))->render()
If you have a named route and want to generate url with query params then:
route('route_name', ['param1' => 'value', 'param2' => 'value']);
If you want to use a controller action directly then:
action('HomeController@index', ['param1'=> 'value', 'param2' => 'value']);
If you want to use an existing url/path and want to append query string then:
url('path') . '?' . http_build_query(['param1' => 'value', 'param2' => 'value']);
i create the url in a blade template
@if(Input::get('page', 0) > 0)
<a href="{{ action('PortController@index', array('page'=>Input::get('page', 0)-1)) }}">next</a> |
@endif
add i want add a param to url
like:
<a href="{{ actionAppend('PortController@index', array('tu'=>'arg'}}">arg</a>
result must be:
http://example.com/ports?page=2&tu=arg
@usman Hmm I'm wondering why they don't make url() behave the same
the best way is add to controller this
request()->merge(['sort_by'=>'favorite','order_by'=>'desc','status'=>1]);
I found this method in Illuminate\Http\Request@fullUrlWithQuery:
request()->fullUrlWithQuery(['y' => 'mx+c'])
@yappkahowe best answer and cleanest one. Thank you!
I have a named route that takes one parameter in the format /route/{orgUnitId} but also takes in query string parameters. I needed to build the url in my blade templates. The above answers are all correct, but my contribution aims to clarify how to deal with these two distinct cases.
My web.php has the route named thus:
Route::get('/route/{orgUnitId}','Controller@showOrgUnit')->name('showOrgUnit');
but the controller looks for additional route parameters in the query string (eg a valid URL is https:mydomain.com/route/23?type=alpha)
My controller uses $request->input('type','defaultType'); to extract the query string parameters while the orgUnitId is passed as a parameter to the showOrgUnit($orgUnitId) function in my controller.
To create this URL I would NOT pass the type=apha key value pair into the named route - This does NOT work:
<a href="{{ route('showOrgUnit',['orgUnitId'=>23,'type'=>'alpha']); }}">Show Org Unit 23 type Alpha</a><!-- DOES NOT WORK -->
Instead I used http_build_query (this is a PHP function, not a laravel helper function) thus:
<a href="{{ route('showOrgUnit',['orgUnitId'=>23]).'?'/.http_build_query(['type'=>'alpha']) }}">Show Org Unit 23 type Alpha</a>
I have built this helper that can be used as a general solution in your whole project:
if (! function_exists('url_add_query_params')) {
/**
* Add query parameters to an existing URL.
*/
function url_add_query_params(string $url, array $params): string
{
// Get an array of the query parameters from the target URL
parse_str(parse_url($url, PHP_URL_QUERY) ?? '', $queryParams);
// Build the new target URL with the query parameters
return Str::of($url)
->before('?')
->append('?', Arr::query(
array_merge($queryParams, $params)
))
->rtrim('?')
->value();
}
}
Tested with PEST like this:
it('adds query params to an existing url', function (string $url, array $params, string $expected) {
expect(url_add_query_params($url, $params))->toBe($expected);
})->with([
'add-to-url-without-queryparams' => ['https://example.com', ['foo' => 'bar'], 'https://example.com?foo=bar'],
'add-to-url-with-queryparams' => ['https://example.com?foo=bar', ['baz' => 'qux'], 'https://example.com?foo=bar&baz=qux'],
'replace-existing-queryparam' => ['https://example.com?foo=bar', ['foo' => 'baz'], 'https://example.com?foo=baz'],
'remove-existing-queryparam' => ['https://example.com?foo=bar', ['foo' => null], 'https://example.com'],
'remove-single-existing-queryparam' => ['https://example.com?foo=bar&baz=qux', ['foo' => null], 'https://example.com?baz=qux'],
'remove-all-existing-queryparam' => ['https://example.com?foo=bar&baz=qux', ['foo' => null, 'baz' => null], 'https://example.com'],
'replace-queryparam-with-empty' => ['https://example.com?foo=bar', ['foo' => ''], 'https://example.com?foo='],
'replace-and-add-queryparams' => ['https://example.com?foo=bar', ['foo' => 'baz', 'baz' => 'qux'], 'https://example.com?foo=baz&baz=qux'],
'replace-param-and-remove-nonexisting' => ['https://example.com?foo=bar', ['foo' => 'baz', 'baz' => null], 'https://example.com?foo=baz'],
]);
Cheers, Pipo
coming soon to Laravel v11.4.1 by @stevebauman 👏
which makes my proposed helper superfluous. You can then just use url()->query()
Please or to participate in this conversation.