What approach should be used in resource type controllers to list and fetch items?

2

I've created the ArticleController controller of type resource , so I have verbs and standard routes . I use the index method of my controller to list all (paginated) articles:

$artigos = Artigo::orderBy('edicao', 'desc')->paginate(25);
return view('painel.artigos.listar', compact('artigos'));

The issue is that in my view listar.blade.php , in addition to listing everything, I also have a search input, which theoretically does the search and populate that same view, so I know the index method of the resource does not accept input parameters, in this case it is best to search for another controller and point back to the view?

I'm using Laravel 5.6

    
asked by anonymous 08.03.2018 / 01:58

1 answer

1

One approach that can be used is to accept query strings to filter on index .

public function index(Request $request) 
{
    $query = $request->query('campo1', 'campo2');
    // Ou todos os campos.. é interessante uma validação aqui
    $query = $request->query();

    $artigos = Artigo::where($query)->orderBy('edicao', 'desc')->paginate(25);

    return view('painel.artigos.listar', compact('artigos'));
}

Your url might look something like this:

/artigos?campo1=123&campo2=234

    
08.03.2018 / 02:14