Laravel Eloquent将大型查询拆分为块并重用它

时间:2018-05-10 13:20:37

标签: php laravel reusability code-reuse laravel-5.6

我真的有一个非常大的查询形式,如下所示。我想拆分它,需要重新使用其他许多ajax调用

$buildquery=Hotel::has('room');

        $buildquery->whereHas('room', function($query) use ($request) {

            // If amenities is there add it to query
            if($request->filled('amenities')){
                $amenities = $request->amenities;
                $count = count($amenities);

                $query->withCount(['amenities' => function($query) use ($amenities, $count){
                                $query->whereIn('amenities_id', $amenities);
                            }])
                            ->having('amenities_count', $count);
                }

                /* filter based on guest */

                if($request->filled('guestsCount')){
                    $memberCount = $request->guestsCount + $request->childCount;
                    $query->Where('capacity', '>=',  $memberCount);
                }else{
                    $query->Where('capacity', '>=',  1);
                }
        });

        $buildquery->with(['room' => function ($query) use ($request) {

            // If amenities is there add it to query
            if($request->filled('amenities')){
                $amenities = $request->amenities;
                $count = count($amenities);

                $query->withCount(['amenities' => function($query) use ($amenities, $count){
                                $query->whereIn('amenities_id', $amenities);
                            }])
                            ->having('amenities_count', $count);
                }

            /* filter based on guest */

            if($request->filled('guestsCount')){
                $memberCount = $request->guestsCount + $request->childCount;
                $query->Where('capacity', '>=',  $memberCount);
            }else{
                $query->Where('capacity', '>=',  1);
            }

            $query->with('roomtype')->with('floorroomcount')->with('image')->with('amenities');

            $query->OrderBy('price');
            $query->Where('astatus', 1)->Where('status', 0);
        }]);


        /* client must be active */
        $buildquery->whereHas('client', function($query) {
            $query->Where('status', 1);
        });

        /* search based on rating */
        if ($request->filled('rating')) {
            if($request->rating > 0){
                $rating = $request->rating;
                    $buildquery->where('star', $rating);
            }
        }

        /* search based on hotel */
        if ($request->filled('location_id')) {
            $buildquery->Where('city', $request->location_id);
        }

        @include('roomlist.area');

       $buildquery->Where('astatus', 1)->where('status', 0); //actually its hotel

       $hotels = $buildquery->simplePaginate(20);

       $hotels = $this->addRates($hotels, $request->checkin_date, $request->checkout_date);
       $hotels = $this->addAvailableCount($hotels, $request->checkin_date, $request->checkout_date);

        $hotels = $hotels->transform(function (Hotel $hotel){

            $hotel->setRelation('room', $hotel->room->sortBy('price')->flatten());

            return $hotel;
        });

        return view('roomlist.loadmore', compact('hotels'));

请在@include('roomlist.area');文件中看到此行roomlist/area.blade.php我有以下代码

<?php
    if($request->filled('type')){
        if($request->type == "Area"){

            //get the area first
            $hotel = Hotel::select('area')->where('city', $request->location_id)->first();
            if(isset($hotel)){
                if($hotel->area != null){
                    $buildquery->where('area', $hotel->area);
                }
            }
        }
    }
?>

有没有办法可以从刀片或任何其他方式包含此代码。

注意:我需要重复使用很多这样的东西。

1 个答案:

答案 0 :(得分:3)

您可以做的第一件事是将一些功能分解为范围:https://laravel.com/docs/5.6/eloquent#query-scopes

例如,你可以改变这个:

    /* search based on hotel */
    if ($request->filled('location_id')) {
        $buildquery->Where('city', $request->location_id);
    }

进入这个:

if ($request->filled('location_id')) {
   $buildquery->inCity($request->location_id);
}

或者这个:

    /* client must be active */
    $buildquery->whereHas('client', function($query) {
        $query->Where('status', 1);
    });

进入这个:

$buildquery->withActiveClient();

这是一个很小的改动,但它允许你在其他地方使用inCity而不需要重写,而对于其他范围,你可以提取更多的代码。

您还可以创建一个Transformer类来更改它:

    $hotels = $hotels->transform(function (Hotel $hotel){

        $hotel->setRelation('room', $hotel->room->sortBy('price')->flatten());

        return $hotel;
    });

对此:

    $hotels = (new HotelRoomTransformer())->transform($hotels);

这种类型的解压缩代码可以使这个文件更具可读性,如果你需要重用它的一部分,你可以将它们放在单独的可重用文件中。

最后,如果要从控制器中完全删除Eloquent,则可以将此类功能全部提取到存储库中。以下是存储库模式的简短指南:https://medium.com/@connorleech/use-the-repository-design-pattern-in-a-laravel-application-13f0b46a3dce