Pagination and data

Published 7 months ago by Mike_91

a

tisuchi
tisuchi
7 months ago (305,135 XP)

@Mike_91

I always strongly suggest not to use the logical part in view. That is the too bad way to write code.

Maybe you can rethink your logic and apply in the controller, at least.

Mike_91

@tisuchi in this situation i cant because i want my data to by dynamic based on user search thisi is the reason.

For my issue you have any idea?

jcmargentina

My friend, you can definetly do that in your controller ... I am totally sure what I am saying.

I wont write you the code ... because I want you to find it out by yourself ... in order to improve your skills.

But please reconsider the design of your code.

tisuchi
tisuchi
7 months ago (305,135 XP)

@Mike_91

I so sorry, but of course there is a way to that. Do research on that. Surely you will get it.

martinbean

@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.

jcmargentina

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.

Good luck

jcmargentina

also in the future ... if you need some dynamic behavior on your site ... consider using AJAX requests, javascript is there just for that.

martinbean

@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 if / 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.

Mike_91

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.

BezhanSalleh

@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.

https://yajrabox.com/docs/laravel-datatables/master/installation

Please sign in or create an account to participate in this conversation.