Filter results using 3 tables in Laravel 5.8?

Asked

Viewed 230 times

0

Create in controller a filter that takes data from 3 different tables. Below I put the relationship and an example of what needs to happen. I am not able to create in controller the filter that should take as reference 02 parameters that will be passed in the URL according to the user’s choice. What’s Working: When I register a Course, I put the Area of Expertise and the Category, this is working and saving well in DB.

Now what I need to do is: On the site, I have a menu that takes the Categories and a submenu that lists the Areas. Type: I stop the mouse in Graduate School and list all the areas it has. I want to filter in the view of courses only those that are Graduate and Area that he clicked.

The URL would look like this in the case, example

http://localhost/SiteNovo/public/pos-graduacao/saude

inserir a descrição da imagem aqui

Relationships are like that:

inserir a descrição da imagem aqui

And mine controller are like this for now:

public function cursos($categoria, $area){

    $cursos = Curso::all();
    $categorias = CategoriaCurso::all();
    $areas = AtuacaoArea::all();

    //Filtrar cursos

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

1 answer

0

In the method cursos class SiteController must have the following signature:

class SiteController extends Controller 
{
     public function cursos($id_categoria_curso, $id_atuacao_area)
     {

     }
}

this will solve your recovery, but be careful with the returned values and criticize them to see if the data really is of the expected type (validate data).


If by any chance these two parameters are of type number can use the restriction part on the route (Regular Expression Constraints), then in your route configuration do:

Route::get('/cursos/{id_categoria_curso}/{id_atuacao_area}', 'SiteController@cursos')
     ->name('cursos')
     ->where(['id_categoria_curso' => '[0-9]+', 'id_atuacao_area' => '[0-9]+']);

Other examples of route restriction:

Text restriction:

Route::get('user/{name}', function ($name) {
    //
})->where('name', '[A-Za-z]+');

Number restriction:

Route::get('user/{id}', function ($id) {
    //
})->where('id', '[0-9]+');

Restriction set:

Route::get('user/{id}/{name}', function ($id, $name) {
    //
})->where(['id' => '[0-9]+', 'name' => '[a-z]+']);

Reference:Laravel - Regular Expression Constraints

To filter the courses with Eloquent:

class SiteController extends Controller 
{
     public function cursos($id_categoria_curso, $id_atuacao_area)
     {
          $cursos = Curso::where('id_categoria_curso', $id_categoria_curso)
              ->where('id_atuacao_area', $id_atuacao_area)
              ->get();
     }
}

In the documentation of Eloquent Builder has the various ways to filter fields from a table, sort and so on.

  • Right, I made the adjustments in Route. For now I’m picking up with id’s same, but then I will pass the names to the url become clearer to the user. But I still don’t understand how SQL would look in the Method courses of the Sitecontroller class. Can you help me?

  • Look @Junioaraujo if you post your 3 templates (Entity) is even answer, but, of course that does not have in your initial question and it is clear that it is always missing to say which relationships they have!

  • You need to put the Models if so and also the class, if not the MER of the relation ! and the expected result @Junioaraujo

  • I changed the question and put examples and prints of what I need. See if you can help me like this. I’m lost in it

  • can you understand my doubt? can you help me?

  • @Junioaraujo already made the edit and it’s there! basically it’s that ... need to take a look at the Laravel Eloquent documentation.

  • It worked like this! Thank you very much for your strength, buddy. Just taking advantage of one thing: If I want to show the names now to not get the id’s in the url, does it change a lot? I have to change course and Eloquent changes a lot?

Show 2 more comments

Browser other questions tagged

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