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
Flávio, I did as you suggested. Only ai ta dando pagina não encontrados quando passo algum parameter. Link changes something?
– Junio Araujo
I received the parameters like this in Function: $id_actuacao_area = $request->id_actuacao_area; $id_categoria_curso = $request->id_categoria_curso;
– Junio Araujo
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
– Flávio Santos
Ready @Flávio Santos, updated everything as you requested!
– Junio Araujo
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 ofcursos/10/15
... I put the numbers 10 and 15 as sample values, but you replace these numbers with PHP variables– Flávio Santos
@Junioaraujo, updated the answer, with the route() function of the Laravel, which mounts the urls in the ideal way. Take a look there!
– Flávio Santos
updated the files as you passed. Now do not render the page. Everything appears blank. No error but load nothing
– Junio Araujo