Published 4 months ago by Mike_91
@Mike_91 The filtering definitely should—and can—be done a controller method. A view is exactly that: a view of your data. The idea is, your controller method passes the data to a view and the view then renders it. Your view should not be doing any logic such as filtering records and performing queries.
If you’re wanting to filter records based on filters supplied by the user, then I strongly suggest watching this lesson: Dedicated Query String Filtering.
Ok, ... if the problem you are having is that you are not getting ALL users, ... please change this on your code:
$search_from = $request->get('date_from'); $search_to = $request->get('date_to'); /*here is the modification*/ $users = User::where('active',1)->with('department', 'article')->get(); /* change the paginate for get*/ $departments = Department::all(); return view('statistics.index', compact(['users', 'departments', 'search_from', 'search_to']));
please ... reconsider the desgin of the code, read the laravel documentation again. Everyhting is there.
@Mike_91 I suggest reading the Laravel documentation on Eloquent and relationships.
If you have a
User that has many
Articles, then you can easily get the counts: https://laravel.com/docs/master/eloquent-relationships#counting-related-models
You also don’t need to put every permutation in
else statements. That’s not maintainable. What happens if you add another field to filter by?
Instead, watch the Laravel lesson I linked you to. It covers your exact scenario.
ok but i dont need that i just need to display all users and the articles they have write in period of time that depends to search.
For example when they launch the page to show them all users and the count of articles they have write today and then in search query to change only the number of articles.
And my bigger problem is that i need to get all of users not only the paginated to export them to csv.
@Mike_91 why don't you use Laravel-DataTables? it will pretty much solve all your problems, filter/search, paginate & exports... feed the query you want and customize the hell out of it.