Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

732
Visualizações
Pass multiple optional parameters in Laravel 9

I am trying to create a route that takes either one of two parameters. However the dd returns me $b_id, null instead of null, $b_id. How can I pass only b as a parameter and omit a?

web.php

Route::get('myroute/{a?}/{b?}', [MyController::class, 'testFunction'])
    ->name('test.testFunction');

Controller

public function testFunction( $a = null,  $b = null) 
{
    dd($a, $b);
    // stuff
}

Ajax call

function test($b_id) {
    $url = "{{ route('test.testFunction', ':b') }}";
    $url = $url.replace(":b", $b_id);
    $.ajax({
        url: $url,
        type: 'GET'
    })}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

This is not really possible at the moment but there is a workaround. You can pass the parameters in an named array and change the route to simply Route::get('myroute' ...):

route('test.testFunction', ['a' => 'xyz', 'b' => 'yzt'])
===== creates
http://myroute?a=xyz&b=yzt

Afterwards you can check if the parameters a or b are present and retrieve them normally if they are.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda