Warning: Undefined array key "options" in /home/cp88alp/domains/arvinlp.ir/public_html/wp-content/plugins/elementor-pro/modules/theme-builder/widgets/site-logo.php on line 194

Hello, I had big problem in my new project, The problem is how handle search or filters side fetching data with Laravel Eloquent. So i searching and find Laravel Search Filters Class, But next problem is limited to get() data and just search and filters! That’s right.

This class wrote for this case but when you writing a project using so many query and this is incorrect code, For example I need user list and next level need search in users or sort users. So in case we have so many Solutions for writing functions :

Solution One :

I have one query for fetch all users or fetch all users sort by x (‘first_name’, ’last_name’, etc) and another query for handle get query and filter last data i fetched. In code :

public function myFunc(Request $request){if($request){$users = User::where(‘first_name’,$request->input(‘first_name’))->orWhere(‘last_name’,$request->input(‘last_name’))->orderBy('id','DESC');}else{$users = User::orderBy('id','DESC')->all();}return $users;}

in this case for sort data by default DESC, We have to add orderBy().

Solution Two :

is using Search Filters Class, so for using this class add class to project and write code like below. in Code :

use App/SearchFilters/SearchFilter;public function myFunc(Request $request){$users = SearchFilter($request, new User);return $users;}
in this case for sort data by default DESC, We have to passed ‘order by’ in Request and can’t be execute, Because this class wrote for searching and filtering.

There have so many solution for fetch and filter data but handle so difficult.

Arvinlp Solution 🙂 is

I develop Search Filter Class for my project and another projects. Have i mistake ?

No, in my SearchFilter Class you can request All Data, Get Data, All Data With Pagination, Or get data by Custom (Like get first or any thing eloquent access me). Is that cool, no ?

Or you Can request data sorted or in sql programming order by x (id, name, etc).

or you need relationships when fetching data so just passed to class and get results.

How ? is Simple for example :

Instead of using this code :

$flight = App\Flight::with('captain')->pageination();

You use this code :

$flight = App\SearchFilter::apply( $request, new Flight, 'pageination', 'captain' );

In above code what happen ?

I told to SearchFilter Class need Apply function and passed all Request, Eloquent Model need fetch data, what type need back data to me, and finally Which relation i need add to my query.

If you think this Class is better way for just go to my github and using data. And help me to develop this class.

More information about this class wrote in github, you can using that.

Thank you.

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *