2017-08-11 1 views
1

私は3つのテーブルがあります。それらの間のlaravel 5.4でデータをフィルタリングする方法は?

  1. products
  2. productcolors
  3. productbrands

関係は以下のとおりです。

  1. productが多くproductcolorsを持っています。
  2. productbrandsは、productに属します。 cviebrock/eloquent-taggable

はすべてproductsは、この依存関係でタグ付けされています。私はすべてがタグ付けされたデータ泳ぐレンダリングするためにこれを使用している

:今

$allproducts = Product::withAllTags('swim')->get(); 

を、私は色やブランドとのデータをフィルタリングしたいが、私はそれを行う方法がわかりません。ここで

は私の製品モデルである:ここで

<?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

ここ

<?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モデルです

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

私のモデルを投稿しました –

+0

@SidHeart 'productColors'をリレーション名' getproductcolor'に置き換え、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

私はこれをしなかったので、私は感謝を知らない –

関連する問題