Reputation:
I am trying to figure out something in Laravel. I have a model 'Phone' and a method countproducts(). Every model 'Phone' has many 'Product'.
I use countproducts() to count the products a phone has.
$phones=Phone::where(function($query){
if(isset($min_weight) && isset($max_weight)){
$query-> where('weight','>=',$min_weight);
$query-> where('weight','<=',$max_weight);
}
This is how i am querying the phones by now.
Is it possible to query and show only phones with lets say more than 50 products??
Thanks in advance
EDIT: 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');
}
Upvotes: 1
Views: 113
Reputation: 883
Straight from the docs.
$phones = Phone::has('products', '>=', 50)->get();
You should be able to put before the get()
your where()
clauses.
Upvotes: 1