我有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