【问题标题】:Laravel Search and FilterLaravel 搜索和过滤
【发布时间】:2022-01-17 03:50:34
【问题描述】:

我使用日期过滤数据后的这个网址(介于两者之间) http://127.0.0.1:8000/viewMonth/filter?_token=9jmqgiLCKNTMKtwObhlVQXCR6Vj1zcrDwsdXDDbm&dateFrom=2022-01-17&dateTo=2022-01-17

现在我正在创建一个名为 SearchData 的新函数,如果我想从过滤日期显示 SearchData,我想怎么做 http://127.0.0.1:8000/viewMonth/filter?_token=9jmqgiLCKNTMKtwObhlVQXCR6Vj1zcrDwsdXDDbm&dateFrom=2022-01-17&dateTo=2022-01-17/{SearchData}

这是我的路线

Route::get('/viewMonth/filter/search', 'App\Http\Controllers\InvoiceController@SearchReportMonth')->name('SearchReportMonth');
Route::get('/viewMonth/filter', 'App\Http\Controllers\InvoiceController@filterDateMonth')->name('filterDateMonth');

【问题讨论】:

    标签: laravel search filter routes


    【解决方案1】:

    我建议只返回带有新过滤数据的页面(除非您需要它更复杂,例如来自 JS 的内容的 HTTP 请求,我假设您知道如何处理)

    一个例子是这样的:

    这只是一个示例,具体实现取决于需求

        /**
         * Display a listing of the resource.
         *
         * @return \Illuminate\Http\Response
         */
        public function index(Request $request)
        {
            $query = User::query();
    
            if (request('search')) {
                $query
                    ->where('name', 'like', '%' . request('search') . '%')
                    ->orWhere('email', 'like', '%' . request('search') . '%')
                    ->orWhere('id', 'like', '%' . request('search') . '%');
            }
    
            if ($request->has(['field', 'sortOrder']) && $request->field != null) {
                $query->orderBy(request('field'), request('sortOrder'));
            }
    
            // Ignore my InertiaJS implementation, will work the same with base Blade Files.
            return Inertia::render('Users/Index', [
                'users' => fn() => $query->paginate(10)->withQueryString(),
            ]);
        }
    

    基本上我使用request() 来获取查询参数,然后从那里构造我的数据库查询并返回它找到的数据。希望这会有所帮助,请务必询问您是否需要更多了解或有疑问!

    【讨论】:

    • 谢谢先生,对我很有帮助
    猜你喜欢
    • 2019-09-02
    • 2021-06-06
    • 2021-05-25
    • 1970-01-01
    • 1970-01-01
    • 2020-04-26
    • 2019-02-12
    • 1970-01-01
    相关资源
    最近更新 更多