doushe7934 2018-05-10 13:20
浏览 121
已采纳

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

I am having really a very big query formation like below. i want to split this and need to re-use for many other ajax call's

$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'));

please see this line @include('roomlist.area'); in that roomlist/area.blade.php file i am having the following code

<?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);
                }
            }
        }
    }
?>

Is there any way that i can include this code from a blade or in any other manner.

Note: i need to re-use many things like this.

  • 写回答

1条回答 默认 最新

  • dongzhazhuo0572 2018-05-10 13:52
    关注

    First thing you could do is break some of this functionality into scopes: https://laravel.com/docs/5.6/eloquent#query-scopes

    For instance, you could change this:

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

    Into this:

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

    Or this:

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

    into this:

    $buildquery->withActiveClient();
    

    This is a small change but it allows you to use inCity in other places without re-writing as much, and for the other scopes it might be more code you can extract.

    You could also make a Transformer class to change this:

        $hotels = $hotels->transform(function (Hotel $hotel){
    
            $hotel->setRelation('room', $hotel->room->sortBy('price')->flatten());
    
            return $hotel;
        });
    

    To this:

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

    This type of extracting code could make this file much more readable, and that way if you need to reuse parts of it you have them in separate, reusable files.

    Lastly, this type of functionality can all be extracted into a repository if you want to entirely remove Eloquent from your controllers. Here is a short guide on the repository pattern: https://medium.com/@connorleech/use-the-repository-design-pattern-in-a-laravel-application-13f0b46a3dce

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,