2017-08-11 24 views
1

我有3個表:如何在laravel 5.4中過濾我的數據?

  1. products
  2. productcolors
  3. productbrands

它們之間的關係是:

  1. 一個product有許多productcolors;
  2. 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'); 
    } 
} 
+0

請出示代碼爲您模型,所以我們知道你的關係是如何定義的。 – Jerodev

回答

1

使用whereHasorWhereHas

$allproducts = Product::whereHas('productColors', function($query) { 
    $query->where('column', 'condition', 'value'); 
})->withAllTags('swim')->get(); 
+0

我發佈了我的模特 –

+0

@SidHeart用您的關係名稱getproductcolor替換'productColors'並更新where條件以符合您的過濾器要求。 – chanafdo

+0

您可以添加查看錶格 –

0

試試這個:

$allproducts = Product::withAllTags('swim')->with(['getproductcolor' => function ($productColorQuery) { 
    $productColorQuery->where('blah', '=', 'blah'); 
}, 'getproductbrand' => function ($productBrandQuery) => { 
    $productBrandQuery->where('blah', '=', 'blah'); 
}])->get(); 
+0

可以告訴我我該如何寫表格 –

0

使用whereHas()

Product::whereHas('colors', function ($q) use ($colorName) { 
     $q->where('name', $colorName); 
    }) 
    whereHas('brands', function ($q) use ($brandName) { 
     $q->where('name', $brandName); 
    }) 
    ->withAllTags('swim') 
    ->get(); 

不要忘記來定義產品模型中的關係:

public function colors() 
{ 
    return $this->hasMany(Color::class); 
} 

public function brands() 
{ 
    return $this->hasMany(Brand::class); 
} 
+0

可以添加視圖嗎?將發佈或得到我從來沒有這樣做,所以我不知道謝謝請 –