Problem with Route and IF in Laravel 5.8

Asked

Viewed 137 times

0

I have a Route that passes 2 parameters, and can be optional.

In the view, If you click on Link 1 (you pass the 2 id’s) I want you to check certain options. If you click on Link 2 (you will pass only 1 id), if you click on Link 3 (you will pass the other id) if you click on Link 4 (you do not pass any to search all the records) In the same View always.

Routes are like this:

Route::group(['namespace' => 'site'], function () {

// Rotas simples do Site

Route::get('/', 'SiteController@index')->name('index');
Route::get('/institucional', 'SiteController@institucional')->name('institucional');
Route::get('/consultoria', 'SiteController@consultoria')->name('consultoria');

Route::get('/seja-revendedor', 'SiteController@revendedor')->name('seja-revendedor');
Route::post('/seja-revendedor/confirmacao', 'SiteController@revendedorSalvar')->name('seja-revendedor.confirmacao');

Route::get('/menus', 'SiteController@menu')->name('menus');

// Rotas dos Cursos
//Route::get('/cursos/{id_categoria_curso?}/{id_atuacao_area?}', 'SiteController@cursosFiltro')->name('cursos');

Route::get('/cursos', 'SiteController@cursosFiltro')->name('cursos');

//Rota para Campo Busca
Route::post('/cursos/busca', 'SiteController@busca')->name('curso.busca'); });

and the Controller is like this:

public function cursosFiltro(Request $request){

    // Lista Cursos, Categorias, Modalidades e Áreas de Atuação
    $categorias = CategoriaCurso::all();
    $modalidades = EnsinoModalidade::all();
    $areas = AtuacaoArea::all();

    $id_atuacao_area = $request->id_atuacao_area;
    $id_categoria_curso = $request->id_categoria_curso;


    if ($request->query('id_categoria_curso') and $request->query('$id_atuacao_area')){
        $cursos = Curso::where('id_categoria_curso', $id_categoria_curso)
            ->where('id_atuacao_area', $id_atuacao_area)
            ->get();
    } elseif ($request->query($id_categoria_curso)){
        $cursos = Curso::where('id_categoria_curso', $id_categoria_curso)->get();

        return view('site.cursos.cursos',
            compact('cursos', 'categorias', 'areas'));
    } elseif ($request->query($id_atuacao_area)) {
        $cursos = Curso::where('id_atuacao_area', $id_atuacao_area)->get();

        return view('site.cursos.cursos',
            compact('cursos', 'categorias', 'areas'));
    }

    $cursos = Curso::all();

    return view('site.cursos.cursos',
        compact('cursos', 'categorias', 'areas'));
}

Link is like this:

1 - Where you pass only one parameter:

@foreach($areas as $area)
    <a href="{{ url("cursos/$area->id_atuacao_area") }}" class="brk-categories__item">
       <span class="brk-categories__item-name">{{ $area->nome }}</span>
    </a>
@endforeach

2 - Where you pass the two parameters:

@foreach($categorias as $categoria)
    <li class="brk-nav__children brk-nav__drop-down-effect">
        <a href="#">
            <span>{{ $categoria->nome }}</span>
        </a>

       <ul class="brk-nav__sub-menu brk-nav-drop-down font__family-montserrat">
           @foreach($areas as $area)
               <li class="dd-effect">
                   <a href="{{ url("cursos/$categoria->id_categoria_curso/$area->id_atuacao_area") }}">
                   <span class="brk-header-list__icon">
                       <i class="{{ $area->icone }}" aria-hidden="true"></i>
                   </span>
                   {{ $area->nome }}
                   </a>
                </li>
           @endforeach
        </ul>
    </li>@endforeach

1 answer

0

The two values cannot be optional if they belong to the route... Because your system will never know which argument was ignored. A nice approach to catch would be the use of querystrings.

Edited: Urls where you pass parameters can be mounted like this:

Where you pass 1 parameter:

<a href="{{ route("cursos", ['id_atuacao_area'=> $area->id_atuacao_area]}}">

Where you pass 2 parameters:

<a href="{{ route("cursos", ['id_atuacao_area'=> $area->id_atuacao_area, 'id_categoria_curso'=> $area->id_categoria_curso]}}">

Your route would look like this:

Route::get('/cursos', 'SiteController@cursosFiltro')->name('cursos');

And your job would stay that way:

  public function cursosFiltro(Request $request){
    // Seu código aqui

    if ($request->query('id_categoria_curso') and $request->query('$id_atuacao_area')){
        $cursos = Curso::where('id_categoria_curso', $id_categoria_curso)
        ->where('id_atuacao_area', $id_atuacao_area)
        ->get();
    }
    // Seu código aqui
}

Its function would receive an argument of the type Request, and you would access the data of querystring using $request->query('id_categoria_curso'), for example.

Take a look here if you want to see more about: https://laravel.com/docs/5.8/requests#retrieving-input

I hope I’ve helped!

  • Flávio, I did as you suggested. Only ai ta dando pagina não encontrados quando passo algum parameter. Link changes something?

  • I received the parameters like this in Function: $id_actuacao_area = $request->id_actuacao_area; $id_categoria_curso = $request->id_categoria_curso;

  • Update the question, and show us your entire route file and the URL you used. it may be that, for some reason, it is not falling on the route created by you

  • Ready @Flávio Santos, updated everything as you requested!

  • The error is in the URL you are mounting. This way I mounted above, your URL will have querystring format: cursos?id_categoria_curso=10&id_atuacao_area=15 instead of cursos/10/15... I put the numbers 10 and 15 as sample values, but you replace these numbers with PHP variables

  • @Junioaraujo, updated the answer, with the route() function of the Laravel, which mounts the urls in the ideal way. Take a look there!

  • updated the files as you passed. Now do not render the page. Everything appears blank. No error but load nothing

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.