Laravel 中的查询方法

querying methods in Laravel

我想在 Laravel 中找出一些东西。 我有一个模型 'Phone' 和一个方法 countproducts()。 每个模型 'Phone' 有很多 'Product'.

我使用 countproducts() 来计算 phone 拥有的产品。

 $phones=Phone::where(function($query){
  if(isset($min_weight) && isset($max_weight)){
      $query-> where('weight','>=',$min_weight);
      $query-> where('weight','<=',$max_weight);
  }

这就是我现在查询 phone 的方式。

是否可以只查询和显示 phones 假设超过 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');
        }
    }

Product.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');
    }

直接来自 docs

$phones = Phone::has('products', '>=', 50)->get();

您应该可以在 get() 之前放置您的 where() 子句。