2014-10-12 117 views
0

如何查詢兩個以上的表格。我想要做的是有一個流派queryed的產品列表,然後查詢productvariations - 在產品變化具有producttype_id = $ producttype-> IDlaravel查詢多個班級或表格

Route::get('browse/{producttype_slug}/{genre_slug}', array(
    'as' => 'products.viewbygenre', 
    function($productype_slug, $genre_slug) 
    {  
     $producttype = ProductTypes::where('slug', '=', $productype_slug)->firstOrFail(); 

     $genre = GenreTypes::where('slug', '=', $genre_slug)->firstOrFail(); 

     // error below 
     $products = Product::with(array('ProductVariations', 'GenreTypes')) 
     ->where('genre', '=', $genre->id)->get(); 

     return View::make('products.viewbygenre')->with(compact('productvariations', 'genre', 'producttype')); 

}))->where('producttype_slug', '[A-Za-z\-]+')->where('genre_slug', '[A-Za-z\-]+'); 

產品類

class Product extends \Eloquent { 
    protected $table = "products"; 
    protected $fillable = ['keywords', 'description', 'title', 'slug', 'release_date', 'clip']; 

    public function productvariations() { 
     return $this->hasMany("ProductVariations"); 
    } 

    public function variations() { 
     $variations = $this->productvariations()->get(); 
     return $variations; 
    } 


    public function genres() { 
     return $this->belongsToMany('GenreTypes', 'product_genretypes', 'product_id', 'genretype_id')->withTimestamps(); 
    } 

} 

回答

0

我不明白的問題,所以我將只是參考此位:

// error below 
$products = Product::with(array('ProductVariations', 'GenreTypes')) 
     ->where('genre', '=', $genre->id)->get(); 

我相信你不能使用where子句對「與」陣列這種方式。我不是100%肯定,如果這個工程當您在使用方法有一個以上的參數,但是這是值得一試:

$products = \Product::with(array('ProductVariations', 'GenreTypes' => function ($query) 
    { 
     // Note I think you also had a mistake in your column name 
     // (genre should be genretype_id?) 
     // Also ... not sure how you are getting the $genre->id variable 
     // as I cannot see the the $genre object/collection in this method you provided ! 
     $query->where('genretype_id', '=', $genre->id); 
    }))->get(); 

讓我們知道您的身體情況如何。