Laravel雄辩的一对多关系,将查询动态添加到相关表中 [英] Laravel Eloquent one to many relationship, dynamically adding query to the related table

查看:84
本文介绍了Laravel雄辩的一对多关系,将查询动态添加到相关表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我昨天在这里提出了一个问题( Laravel口才查询构建选择最小值).从那开始,我正在更新查询以选择房间.

I have asked a question here yesterday (Laravel Eloquent query build select min value). From that i am updating my query to select rooms.

我的上一个查询:

$buildquery=Room::

        with(['hotel' => function ($query) {
            $query->where('status', 0);
        }])

        ->with('image')->with('amenities');

        if ($request->filled('location_id')) {

            $buildquery->Where('city', $request->location_id);
            //print_r($request->location_id);
        }

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

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


        // If price is there add it to query
        if ($request->filled('min_price')) {
            $buildquery->whereBetween('price', [$request->min_price, $request->max_price]);    
        }

        $buildquery->Where('astatus', 1)->Where('status', 0);

        //$buildquery->orderBy('price', 'DESC')->take(1);

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

我更新的uqery:

$rooms = Hotel::with(['room' => function($query) {
            $query->orderBy('price', 'asc')->first();
        },
        'room.image',
        'room.amenities'])
        ->get();

我遇到一个问题. 如果用户选择了便利设施,则只有我包括以下查询,

I ran into one problem. If the user selects the amenities, then only i am including the following query,

if($request->filled('amenities')){
                $amenities = $request->amenities;
                $count = count($amenities);

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

如何将其添加到更新的查询中?

尝试过但没有运气

$rooms = Hotel::with(['room' => function($query) {
            $query->orderBy('price', 'asc')->first();
        },
        'room.image',
        'room.amenities' => function($query) {
            $query->withCount(['amenities' => function($query) use ($amenities){
                            $query->whereIn('amenities_id', $amenities);
                        }])
                        ->having('amenities_count', $count);
        }])->get();

推荐答案

我认为,您正在寻找"whereHas"构建器函数. https://laravel.com/docs/5.5/eloquent-relationships#查询关系存在

I think, you are looking for 'whereHas' builder function. https://laravel.com/docs/5.5/eloquent-relationships#querying-relationship-existence

$rooms = Hotel::with(['room' => function($query) {
            $query->orderBy('price', 'asc')->first();
        },
        'room.image',
    }])
    ->whereHas('amenities', function ($query) use ($amenities) {
        $query->whereIn('id', $amenities);
    })
    ->get();

这篇关于Laravel雄辩的一对多关系,将查询动态添加到相关表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆