Get ID of the NAME parameter passed on the route with Laravel

Asked

Viewed 818 times

0

I have a route that has parameters:

Route::get('/cursos/{area?}/{categoria?}', 'SiteController@cursosFiltro')->name('cursos');

In the URL to passing the Slug of these past terms:

{{ url("cursos?categoria=$categoria->slug&area=$area->slug")}}

Now, I need to get the id’s on those slugs from the Controller. I need to take the CATEGORY table the id of the Slug coming in the url and take the table AREA tb and assign this value to a variable.

I did so:

public function cursosFiltro(Request $request, $area = '', $categoria = ''){ 
    $categoria = ($request->categoria);
    $area = ($request->area);

    $idCategoria = CategoriaCurso::where('id_categoria_curso', $categoria)->get();
    $idArea = AtuacaoArea::get('id_atuacao_area', $area)->get();

    return view('site.cursos.cursos')
        ->with(compact('cursos', 'idArea', 'idCategoria'));}

It’s not working. I can’t get the Slug id from the url. How to do?

  • Are you storing Slug in a database? What’s the column called? Or dynamically create?

  • Yes! stored in the @Miguel database

  • Try this: CategoriaCurso::where('col_do_slug', $request->categoria)->first();

1 answer

0

When you create the route this way:

Route::get('/cursos/{area?}/{categoria?}', 'SiteController@cursosFiltro')->name('cursos');

You specify that it is an optional wall meter.

That way I’d use it on the route:

{{ url("cursos/$categoria->slug/$area->slug")}}

This way the controller will recover the data as follows:

public function cursosFiltro(Request $request, $area, $categoria){ 

    $idCategoria = CategoriaCurso::where('id_categoria_curso', $categoria)->first();
    $idArea = AtuacaoArea::get('id_atuacao_area', $area)->first();

    return view('site.cursos.cursos')
        ->with(compact('cursos', 'idArea', 'idCategoria'));
}

Any doubt just comment.

Browser other questions tagged

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