1

This will be obvious to someone else

I have a route that works and goes to the correct controller

Route::get('v1/holidays/{country}/{year}/{month}/{official?}',
'retrieveHolidayController@test'

so if i go to http://example.com/v1/holidays/US/2014/03/01

it will go where I want to go

however I want the link to look like

http://example.com/v1/holidays?country=US&year=2014&month=03&official=01

How can I do this please ?

3 Answers 3

1

You redefine your route to

Route::get('v1/holidays', 'retrieveHolidayController@test');

Then in your controller you can get the param values with $request

public function test(Request $request)
{
    if ( $request->has('country') && $request->country != '') {
        $country = $request->country;
    }

    if ( $request->has('year') && $request->year != '') {
        $year = $request->year;
    }

    .... // and the others. Then you can query like this

    $holidays = Holiday::when($country, function($query) use ($country) {
        return $query->where('country', $country);
    })
    ->when($year, function($query) use ($year) {
        return $query->where('year', $year);
    })
    ->get();

    //Using 'when' only executes the closure if the variable exists
}

Now, you can use your URL just the way you wanted:
http://example.com/v1/holidays?country=US&year=2014&month=03&official=01

Sign up to request clarification or add additional context in comments.

Comments

0

Make country,year and monthalso optional:-

Route::get('v1/holidays/{country?}/{year?}/{month?}/{official?}', 'retrieveHolidayController@test')

Comments

0
Route::get('v1/holidays', 'retrieveHolidayController@test');

Route::get('v1/holidays/{country}/{year}/{month}/{official?}', function($country){
    return redirect()->to(action('retrieveHolidayController@test', ["country"=>$country,......]));
});

access to http://example.com/v1/holidays/US/2014/03/01

redirect to http://example.com/v1/holidays?country=US&year=2014&month=03&offical=01

if official param is null redirect param offical= param is nullable

not feel good so

isset($official){
     $paramArry["official"] = $official;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.