[英]Laravel change get parameter in url string and return
I'm using get parameters for search filters, and need to be able to change a value of the query string variable and return the modified url (as a string variable, nothing fancy, no redirect or anything).我正在为搜索过滤器使用获取参数,并且需要能够更改查询字符串变量的值并返回修改后的 url(作为字符串变量,没什么特别的,没有重定向或任何东西)。 This is what I have so far and what is happening:
这是我到目前为止所拥有的以及正在发生的事情:
public function index(Request $request){
echo $request->fullUrl();
// outputs https://test.com/search?type=somestring
$request->merge(['type' => 'anotherstring']);
echo $request->fullUrl();
// still outputs https://test.com/search?type=somestring
// is there a way to change a parameter value in the url and
// return the modified url string?
}
I figure if worse comes to worse, I'll just parse the string manually, but it feels like there's a "laravel way" to go about this that I happen to be missing?我想如果情况变得更糟,我只会手动解析字符串,但感觉有一种“laravel 方式”可以解决这个问题,但我碰巧错过了?
请改用fullUrlWithQuery
。
echo $request->fullUrlWithQuery(['type' => 'anotherstring']);
更糟糕的情况你可以这样做:
echo url()->current().(!empty($request->all())?"?":"").http_build_query($request->all());
if you need to keep some previous parameter and replace/add others:如果您需要保留一些以前的参数并替换/添加其他参数:
$request->fullUrlWithQuery(array_merge($request->all(),['overwritten'=>'changed_value','added'=>'new_value']));
it replaces the query with a merge of previous parameters, adding and overwriting with the new parameters它用先前参数的合并替换查询,添加和覆盖新参数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.