Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Jacobs's avatar

Redirect back with query string parameters

Hey, is it possible to redirect back with query string parameters? Based on datepicker I choose a date, then with Carbon I get the week and year from the date. After that I want to redirect back to the previous page with new query string parameters. Keep in mind I didn't pass the week and year parameters into the controller. Currently using this:

return redirect(strtok(back()->getTargetUrl(),'?') . '?week=' . $week . '&year=' . $year);

and using back()->with(['week' => $week, 'year' => $year]); has no effect

0 likes
2 replies
jlrdw's avatar

Store url and uri in session, and just link to where you were.

$areturn = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$haystack = $areturn;
$needle   = '?';
$pos      = strripos($haystack, $needle);
if ($pos === false) {
    $areturn = $areturn."?psch=" . Session::get('dogsearch') . "&aval=" . Session::get('dogaval') . "&page=1" ;
    Session::put('areturn', $areturn);
    
} else {
   
    Session::put('areturn', $areturn);
}

$areturn can now be used in an a href tag. Also works for redirect. If parameters used instead of querystring, just build $areturn with the parameters.

I don't have a quick example from laravel, but here is another way I would do it in Yii2, would be same in laravel:

return $this->redirect(['dog/indexadmin?p=' . Yii::$app->session->get('dogpage') . '&psch=' . Yii::$app->session->get('dogsearch') . '&aval=' . Yii::$app->session->get('dogaval')]);

I prefer this way as I generally code with the what if I have to change frameworks.

I recently converted a cakephp to laravel. Took just a few hours instead of a week.

There were querybuilder differences, but minor.

Cronix's avatar

Does this work?

$url = back()->getTargetUrl() . '?week=' . $week . '&year=' . $year;
redirect($url);

Please or to participate in this conversation.