我正试图在Laravel中找到一些东西。我有一个模型"Phone"和一个方法countproducts()。每个型号的"手机"都有许多"产品"。
我使用countproducts()来计算手机的产品数量。
$phones=Phone::where(function($query){
if(isset($min_weight) && isset($max_weight)){
$query-> where('weight','>=',$min_weight);
$query-> where('weight','<=',$max_weight);
}
这就是我现在查询手机的方式。
是否可以只查询和显示50多种产品的手机??
提前感谢
编辑:Phone.php
<?php namespace App;
use Illuminate'Database'Eloquent'Model;
class Phone extends Model {
public function productnum()
{
return $this->hasMany('App'Product');
}
public function productStore()
{
return $this->hasOne('App'Store');
}
public function category(){
return $this->belongsTo('App'Category');
}
public function minprice(){
return $this->productnum->min('newprice');
}
public function maxprice(){
return $this->productnum->max('newprice');
}
}
产品.php
<?php namespace App;
use Illuminate'Database'Eloquent'Model;
class Product extends Model {
public function manufacturer(){
return $this->hasOne('manufacturer');
}
public function phone(){
return $this->belongsto('App'Phone');
}
public function category(){
return $this->belongsto('App'Category');
}
public function store(){
return $this->belongsto('App'Store');
}
直接从文档中获取。
$phones = Phone::has('products', '>=', 50)->get();
你应该能够把你的where()
子句放在get()
之前。