如何在laravel 5.2中的多个关联表中的条件

时间:2016-11-23 07:30:00

标签: php laravel laravel-5.2

我正面临一个问题。谁能帮我 首先我有两个模型

1)user.php的

enter code here
public function listings(){
    return $this->hasOne('App\Listing','user_id')
          ->with("topservices");
}

现在我有清单模型

2)(Listing.php)

enter code here
public function topservices(){
    return $this->hasMany('App\Sellerprofile','list_id');
}

现在我有一个功能

enter code here
public function nails(){
    $users = DB::table('users')
        ->where(['service_name'=>"Seller"])
        ->get();
    $users = json_decode(json_encode($users),true);
    foreach($users as $alluser){
        $ids[] = $alluser['id']; 
    }
    if(!empty($ids)){
            $allData = User::with('listings')->whereIn('id',$ids)->get();
            $allData = json_decode(json_encode($allData),true);
            echo "<pre>"; print_r($allData); die; ///when i print this array it gives me below output

    } 
    $title = "Nails";
    return view("listings.listing",['allData'=>$allData,'title'=>$title]);
}

输出:

enter code here
Array
(
[0] => Array
    (
        [id] => 1
        [title] => Mr
        [firstname] => kunal
        [email] => kunal@gmail.comsc
        [listings] => Array
            (
                [id] => 1
                [user_id] => 1
                [type] => premimum
                [business_name] => kunal
                [topservices] => Array
                    (
                        [0] => Array
                            (
                                [id] => 5
                                [service_id] => 1
                                [list_id] => 1
                                [name] => Acrylic 
                                [duration] => 10mins
                                [price] => 10
                            )

                        [1] => Array
                            (
                                [id] => 6
                                [service_id] => 2
                                [list_id] => 1
                                [name] => Hair Top
                                [duration] => 30mins
                                [price] => 20
                            )

                        [2] => Array
                            (
                                [id] => 7
                                [service_id] => 3
                                [list_id] => 1
                                [name] => Skin Care Top
                                [duration] => 1hr-30mins
                                [price] => 30
                            )

                        [3] => Array
                            (
                                [id] => 8
                                [service_id] => 4
                                [list_id] => 1
                                [name] => Massage Top
                                [duration] => 20mins
                                [price] => 50
                            )

                    )
            )

    )

[1] => Array
    (
        [id] => 2
        [title] => Mr
        [firstname] => kunal
        [email] => kunal@gmail.comsdsfd
        [listings] => Array
            (
                [id] => 2
                [user_id] => 2
                [type] => premimum
                [topservices] => Array
                    (
                        [0] => Array
                            (
                                [id] => 9
                                [service_id] => 6
                                [list_id] => 2
                                [name] => Acrylic 
                                [duration] => 30mins
                                [price] => 10
                            )

                        [1] => Array
                            (
                                [id] => 10
                                [service_id] => 6
                                [list_id] => 2
                                [name] => Powder gel nails 
                                [duration] => 45mins
                                [price] => 20
                            )

                        [2] => Array
                            (
                                [id] => 11
                                [service_id] => 6
                                [list_id] => 2
                                [name] => Polish change
                                [duration] => 10mins
                                [price] => 25
                            )

                        [3] => Array
                            (
                                [id] => 12
                                [service_id] => 6
                                [list_id] => 2
                                [name] => Hand design
                                [duration] => 2hrs
                                [price] => 100
                            )

                        [4] => Array
                            (
                                [id] => 13
                                [service_id] => 7
                                [list_id] => 2
                                [name] => Hair Top
                                [duration] => 30mins
                                [price] => 200
                            )

                    )
            )

    )
)

现在您看到上面的输出有Topservices数组我希望所有那些数组在topservices下的价格大于= 10且小于等于30 我的预期输出是: -

enter code here
Array
(
[0] => Array
    (
        [id] => 1
        [title] => Mr
        [firstname] => kunal
        [email] => kunal@gmail.comsc
        [listings] => Array
            (
                [id] => 1
                [user_id] => 1
                [type] => premimum
                [business_name] => kunal
                [topservices] => Array
                    (
                        [0] => Array
                            (
                                [id] => 5
                                [service_id] => 1
                                [list_id] => 1
                                [name] => Acrylic 
                                [duration] => 10mins
                                [price] => 10
                            )

                        [1] => Array
                            (
                                [id] => 6
                                [service_id] => 2
                                [list_id] => 1
                                [name] => Hair Top
                                [duration] => 30mins
                                [price] => 20
                            )
                    )
            )

    )

[1] => Array
    (
        [id] => 2
        [title] => Mr
        [firstname] => kunal
        [email] => kunal@gmail.comsdsfd
        [listings] => Array
            (
                [id] => 2
                [user_id] => 2
                [type] => premimum
                [topservices] => Array
                    (
                        [0] => Array
                            (
                                [id] => 9
                                [service_id] => 6
                                [list_id] => 2
                                [name] => Acrylic 
                                [duration] => 30mins
                                [price] => 10
                            )

                        [1] => Array
                            (
                                [id] => 10
                                [service_id] => 6
                                [list_id] => 2
                                [name] => Powder gel nails 
                                [duration] => 45mins
                                [price] => 20
                            )

                        [2] => Array
                            (
                                [id] => 11
                                [service_id] => 6
                                [list_id] => 2
                                [name] => Polish change
                                [duration] => 10mins
                                [price] => 25
                            )

                    )
            )

    )
)

任何人都可以帮助我。在此先感谢我在指甲功能方面做了什么

2 个答案:

答案 0 :(得分:0)

您可以更改方法以包含条件 您可以使用whereBetween方法:

public function listings($lowerPriceLimit = null,$upperPriceLimit = null){
    $query = $this->hasOne('App\Listing','user_id');
    if($lowerPriceLimit != null &&  $upperPriceLimit != null)
        $query = $query->with(["topservices"=>function($q) use($lowerPriceLimit,$upperPriceLimit ){
                            $q->whereBetween('price',[$lowerPriceLimit,$upperPriceLimit]);
                        }]);
    else
        $query->with("topservices");
    return $query;
}

答案 1 :(得分:0)

在这里,我找到了自己的解决方案。

enter code here
$allData = User::with(['listings.topservices'=>function($query){
           $query->whereBetween('price',[10,350]);
           }])
           ->whereIn('id',$ids)->get();