我有3个表:如何在laravel 5.4中过滤我的数据?
products
productcolors
productbrands
它们之间的关系是:
- 一个
product
有许多productcolors
; productbrands
属于product
。
所有products
都标记为此依赖关系:cviebrock/eloquent-taggable
。
我已经使用这个渲染所有游泳标记的数据:
$allproducts = Product::withAllTags('swim')->get();
现在,我要为过滤与色彩和品牌的数据,但我不知道该怎么做。
这里是我的产品型号:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Cviebrock\EloquentTaggable\Taggable;
use Carbon\Carbon;
class Product extends Model
{
use Taggable;
protected $table ='products';
protected $dates = ['published_at'];
public function setPublishedAtAttribute($date) {
$this->attributes['published_at'] = Carbon::createFromFormat('Y-m-d', $date);
}
public function scopePublished($query) {
$query->where('published_at', '<=', Carbon::now());
}
public function scopeUnpublished($query) {
$query->where('published_at', '>', Carbon::now());
}
public function scopeNewin($query) {
$query->where('published_at', '>', Carbon::now()->subDays(7));
}
public function getCreatedAtAttribute($date) {
return $this->asDateTime($date)->toFormattedDateString();
}
public function getproductcolor() {
return $this->hasMany('App\productcolor');
}
public function getproductimage() {
return $this->hasMany('App\productimage');
}
public function getproductbrand() {
return $this->hasMany('App\productbrand');
}
public function getproductsize() {
return $this->hasMany('App\size');
}
}
这里是我的productcolor
型号:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Cviebrock\EloquentTaggable\Taggable;
class productcolor extends Model
{
use Taggable;
public function getproductcolor() {
return $this->belongsTo('App\Product');
}
}
这里是我的productBrands
型号:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Cviebrock\EloquentTaggable\Taggable;
class productbrand extends Model
{
use Taggable;
public function productBrand() {
return $this->belongsTo('App\Product');
}
}
请出示代码为您模型,所以我们知道你的关系是如何定义的。 – Jerodev