007. Eloquent 条件查询——tucker-eric/eloquentfilter
Eloquent 条件查询——tucker-eric/eloquentfilter
搜索是每个项目中必不可少的功能,我们需要通过不同的请求数据,筛选出想要的数据,有时候你会发现查询条件很多的时候,Controller 中的代码会越来越多,你可能会想优化一下这部分逻辑,tucker-eric/eloquentfilter 就是这样一个扩展包,帮助我们优化搜索这部分逻辑。
场景分析
先来看一下 LaraBBS 中的一个场景,话题列表接口
中我们已经实现了根据分类 ID category_id
筛选话题,根据 order
参数排序。打开 app/Http/Controllers/Api/TopicsController.php
看看代码实现。
app/Http/Controllers/Api/TopicsController.php
class TopicsController extends Controller
{
public function index(Request $request, Topic $topic)
{
$query = $topic->query();
// 搜索分类
if ($categoryId = $request->category_id) {
$query-&...