Skip to content

Instantly share code, notes, and snippets.

@nmfzone
Last active April 9, 2019 16:20
Show Gist options
  • Select an option

  • Save nmfzone/c83e4a50e95717057a4af9100bae1db9 to your computer and use it in GitHub Desktop.

Select an option

Save nmfzone/c83e4a50e95717057a4af9100bae1db9 to your computer and use it in GitHub Desktop.
Laravel Redirect to Query String
<?php
namespace App\Providers;
use Illuminate\Support\Arr;
use Illuminate\Routing\Redirector;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
/**
* Create a new redirect response to the given queries.
*
* @param array $queries
* @param bool $merge
* @param int $status
* @param array $headers
* @param bool $secure
* @return \Illuminate\Http\RedirectResponse
*/
Redirector::macro('toQuery', function ($queries, $merge = true, $status = 302, $headers = [], $secure = null) {
$request = $this->generator->getRequest();
$queries = Arr::query($merge ? array_merge($request->query(), $queries) : $queries);
return $this->to($request->url().'?'.$queries, $status, $headers, $secure);
});
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
<?php
namespace App\Http\Controllers;
class HomeController extends Controller
{
/**
* Display the listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return redirect()->toQuery(['slug' => 'Post'], false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment