我怎样才能使这段代码简单。我将此 query() 函数用于许多部分

How can I make this code easy . i use this query() function for many section

** 我怎样才能使这段代码简单。我将此 query() 函数用于许多部分 我想通过过滤器获取事件。我的过滤器基于标签和类别(两者都在枢轴 table 中) **

public function index(Request $request)
{
    $customQuery = Event::where('status', '1');
    if ($request->vehical_type) { //vehical filter
        $customQuery->where('vehical_type', '=', $request->vehical_type);
    }
    if ($request->tag) { //tag filter
        $id = $request->tag;
        $customQuery->whereHas('tags', function (Builder $query) use ($id) {
            $query->where('tag_id', $id);
        });
    }
    if ($request->category) { //category filter
        $id = $request->category;
        $customQuery->whereHas('categories', function (Builder $query) use ($id) {
            $query->where('category_id', $id);
        });
    }
    $events = $customQuery->get(); //get event according to filters
    $tags = Tag::whereHas('events', function (Builder $query) { //get all tags of events
        $query->where('event_id', '>', 0);
    })->get();
    $categories = Category::whereHas('events', function (Builder $query) { //get all categories of events
        $query->where('event_id', '>', 0);
    })->get();
    return view('frontend.event.index', compact('events', 'tags', 'categories'));
}

您可以使用 whereRelation 获取而不是使用 whereHas。

$customQuery->whereRelation('tags', 'tag_id', $id)

使用此结构,您可以缩短代码并进行优化。

新版本新增关系查询方式check here

您可以将 when 用于查询中的条件,并使用 fn 的简写闭包将它们放在一行中。 whereRelation 而不是 whereHas 也可以使这些查询更短一些。

public function index(Request $request)
{
    $events = Event::query()
        ->where('status', '1')
        ->when(
            $request->vehical_type,
            fn($query) => $query->where('vehical_type', $request->vehical_type)
        )
        ->when(
            $request->tag,
            fn($query) => $query->whereRelation('tags', 'tag_id', $request->tag)
            // or fn($query) => $query->whereHas('tags', fn($tags) => $tags->where('tag_id', $request->tag))
        )
        ->when(
            $request->category,
            fn($query) => $query->whereRelation('categories', 'category_id', $request->category)
            // or fn($query) => $query->whereHas('categories', fn($category) => $category->where('category_id', $request->category))
        )
        ->get();

    $tags = Tag::query()
        ->whereRelation('events', 'event_id', '>', 0)
        // or ->whereHas('events', fn($events) => $events->where('event_id', '>', 0))
        ->get();

    $categories = Category::query()
        ->whereRelation('events', 'event_id', '>', 0)
        // or ->whereHas('events', fn($events) => $events->where('event_id', '>', 0))
        ->get();

    return view('frontend.event.index', compact('events', 'tags', 'categories'));
}

您也可以使用查询范围将搜索放在 Event 模型中。

# Event.php
public function scopeSearch(Builder $query, array $filter = [])
{
    return $query
        ->when(
            array_key_exists('vehicle_type', $filter),
            fn($query) => $query->where('vehical_type', $filter['vehicle_type'])
        )
        ->when(
            array_key_exists('tag', $filter),
            fn($query) => $query->whereRelation('tags', 'tag_id', $filter['tag'])
            // or fn($query) => $query->whereHas('tags', fn($tags) => $tags->where('tag_id', $filter['tag']))
        )
        ->when(
            array_key_exists('category', $filter),
            fn($query) => $query->whereRelation('categories', 'category_id', $filter['category'])
            // or fn($query) => $query->whereHas('categories', fn($category) => $category->where('category_id', $filter['category']))
        );
}
public function index(Request $request)
{
    $filter = [
        'vehicle_type' => $request->vehicle_type,
        'tag' => $request->tag,
        'category' => $request->category,
    ];

    $events = Event::query()
        ->where('status', '1')
        ->search($filter)
        ->get();

    $tags = Tag::query()
        ->whereHas('events', fn($events) => $events->where('event_id', '>', 0))
        ->get();

    $categories = Category::query()
        ->whereHas('events', fn($events) => $events->where('event_id', '>', 0))
        ->get();

    return view('frontend.event.index', compact('events', 'tags', 'categories'));
}
    // Get event according to filters
    $events = Event::where('status', '1')
        ->when($request->get('vehicle_type'), function ($query, $vehicleType) {
            $query->where('vehical_type', '=', $vehicleType);
        })
        ->when($request->get('tag'), function ($query, $tagId) {
            $query->whereHas('tags', function ($query) use ($tagId) {
                $query->where('tag_id', $tagId);
            });
        })
        ->when($request->get('category'), function ($query, $categoryId) {
            $query->whereHas('categories', function ($query) use ($categoryId) {
                $query->where('category_id', $categoryId);
            });
        })
        ->get();

对于这些类型的情况,您可以使用 when 方法。您可以查看文档 here.