Php 如何在laravel 5.2中的多个关联表中设置条件
我面临一个问题。有人能帮我吗 首先我有两个模型 1) User.php 现在我有了列表模型 2) (Listing.php) 现在我有一个函数Php 如何在laravel 5.2中的多个关联表中设置条件,php,laravel,laravel-5.2,Php,Laravel,Laravel 5.2,我面临一个问题。有人能帮我吗 首先我有两个模型 1) User.php 现在我有了列表模型 2) (Listing.php) 现在我有一个函数 enter code here public function nails(){ $users = DB::table('users') ->where(['service_name'=>"Seller"]) ->get(); $users = json_decode(json_encod
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]);
}
现在你们看到了上面的输出,这里有一个TopService数组,我想要TopService下的所有数组,它们的价格大于等于10,小于等于30
我的预期产出是:-
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;
}
有人能帮我吗。提前感谢我在钉子功能中所做的工作您可以更改您的方法以包括条件。
您可以使用
whereBetween
方法:
enter code here
$allData = User::with(['listings.topservices'=>function($query){
$query->whereBetween('price',[10,350]);
}])
->whereIn('id',$ids)->get();
在这里,我找到了自己的解决办法
@jaysingkar-its-giving-me-error不起作用(explode()期望参数2为字符串,对象为给定):(您使用whereBetween-price列中的price不在列表表中,它在topservices中,希望您理解它给了我错误(“where子句”中的未知列“price”)(SQL:在(1、2)和10到30之间的
价格中选择*fromlistings
其中listings
用户id
)
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
)
)
)
)
)
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
)
)
)
)
)
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;
}
enter code here
$allData = User::with(['listings.topservices'=>function($query){
$query->whereBetween('price',[10,350]);
}])
->whereIn('id',$ids)->get();