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

Sema314's avatar

Applying a query string to a Route

So I have the following route configured that when I visit URL/employees/import it works great and the functionality does what I need it to do:

Route::get('employees/import', [ 'as' => 'employees.import', 'uses' => 'EmployeesController@import' ]);

Here is the EmployeesController class:

class EmployeesController extends Controller
{
    protected $employeeImporter;

    public function __construct(EmployeeImporter $employeeImporter)
    {
        $this->middleware('auth', ['except' => ['import']]);
        $this->employeeImporter = $employeeImporter;
    }

    public function index()
    {
        $employees = Employee::all();

        return view('employees.index', compact('employees'));
    }

    public function show(Employee $employee)
    {
        return view('employees.show', compact('employee'));
    }

    public function update(EmployeeRequest $request, Employee $employee)
    {
        $employee->update($request->all());

        if (count($request->files) > 0) {
            $employee->touch();
        }

        return redirect()->route('employees.show', $employee->id);
    }

    public function import()
    {
        $this->employeeImporter->import();
    }
}

What I'm attempting to achieve:

So since the default call is employees/import - How can I make it be based on a query string? So I'd like the default to be employees/import?mode=update, but also have the option to have employees/import?mode=refresh that depends on another method inside the class?

All help would be appreciated! Learning and new to Laravel.

0 likes
8 replies
bobbybouwmann's avatar

You can do something like this in your controller

public function import(Request  $request)
{
    $mode = $request->query('mode', 'update');

    // Your code here
}

This first argument is the name of the key, the second argument is the default value.

Sema314's avatar

Would I have to make any indication on the route? employees/import/?mode={{mode}}

martinbean's avatar

@sema314 You don’t specify query strings as part of route definitions. Routes are based on the URI only.

I’d also not use the query string to invoke different actions. Make them their own, individual routes instead. It’ll make your code leaner and easier to navigate.

Sema314's avatar

Do you by any chance have an example @martinbean? Apologies, just got into Laravel like two days ago and working on an existing project.

bobbybouwmann's avatar

@sema314 He means something like this

Route::get('employees/import/{?mode}', [ 'as' => 'employees.import', 'uses' => 'EmployeesController@import' ]);

Then in your controller you can specify the default value

public function import($mode = 'update')
{
}
jlrdw's avatar

Also, you don't have to apply a query string to a route, it's a global, part of HTTP. Whereas parameters are different.

somsite.com?firstname=bob&lastname=smith

Don't need a route parameter. It's in the request anyway.

somsite.com/bob/smith

Here bob and smith are route parameters.

Already answered above, just wanted to show an example.

martinbean's avatar

@sema314 If you have code that does different things, make them their own routes:

Route::post('employees/import', 'EmployeeImportController@store');
Route::put('employees/update', 'EmployeeImportController@update');

Don’t try and shoe-horn things into one, single route as then your controllers just end up being full of if statements and other conditionals.

RamjithAp's avatar

You cant have one route assigned to multiple methods in controllers. So do this

Route::get('employees/import/{mode}', [ 'as' => 'employees.import', 'uses' => 'EmployeesController@import' ]);

public function import($mode){
  if($mode=='update'){
   	//code
  }elseif($mode=='refresh'){
	//code
  }
}

OR

Route::get('employees/import/update', [ 'as' => 'employees.import.update', 'uses' => 'EmployeesController@update' ]);
Route::get('employees/import/refresh', [ 'as' => 'employees.import.refresh', 'uses' => 'EmployeesController@refresh' ]);

public function update(){

}
public function refresh(){

}

Please or to participate in this conversation.