2013-04-04 104 views
4

我想學習Laravel,現在我正在試驗雄辯。 我有,我不能現在解決下面的情況:Laravel雄辯許多到許多如何

比方說,我創建一個網上商店,所以我有產品(產品1,產品2等):

+-------------+------------------+------+-----+---------------------+----------------+ 
| Field  | Type    | Null | Key | Default    | Extra   | 
+-------------+------------------+------+-----+---------------------+----------------+ 
| id   | int(10) unsigned | NO | PRI | NULL    | auto_increment | 
| name  | varchar(255)  | NO |  | NULL    |    | 
| slug  | varchar(255)  | NO |  | NULL    |    | 
| description | text    | NO |  | NULL    |    | 
| price  | decimal(5,2)  | NO |  | NULL    |    | 
| active  | tinyint(4)  | NO |  | 1     |    | 
| created_at | timestamp  | NO |  | 0000-00-00 00:00:00 |    | 
| updated_at | timestamp  | NO |  | 0000-00-00 00:00:00 |    | 
+-------------+------------------+------+-----+---------------------+----------------+ 

這些產品被分類成分類標準(品牌,顏色,部門)

+-------------+------------------+------+-----+---------------------+----------------+ 
| Field  | Type    | Null | Key | Default    | Extra   | 
+-------------+------------------+------+-----+---------------------+----------------+ 
| id   | int(10) unsigned | NO | PRI | NULL    | auto_increment | 
| name  | varchar(255)  | NO |  | NULL    |    | 
| slug  | varchar(255)  | NO |  | NULL    |    | 
| description | text    | NO |  | NULL    |    | 
| created_at | timestamp  | NO |  | 0000-00-00 00:00:00 |    | 
| updated_at | timestamp  | NO |  | 0000-00-00 00:00:00 |    | 
+-------------+------------------+------+-----+---------------------+----------------+ 

這些分類有條款(耐克,阿迪達斯,紅色,綠色,男孩,女孩):

+-------------+------------------+------+-----+---------------------+----------------+ 
| Field  | Type    | Null | Key | Default    | Extra   | 
+-------------+------------------+------+-----+---------------------+----------------+ 
| id   | int(10) unsigned | NO | PRI | NULL    | auto_increment | 
| taxonomy_id | int(11)   | NO |  | NULL    |    | 
| name  | varchar(255)  | NO |  | NULL    |    | 
| slug  | varchar(255)  | NO |  | NULL    |    | 
| description | text    | NO |  | NULL    |    | 
| created_at | timestamp  | NO |  | 0000-00-00 00:00:00 |    | 
| updated_at | timestamp  | NO |  | 0000-00-00 00:00:00 |    | 
+-------------+------------------+------+-----+---------------------+----------------+ 

而且最後我有一個數據透視表的條款連接產品:

+------------+------------------+------+-----+---------------------+----------------+ 
| Field  | Type    | Null | Key | Default    | Extra   | 
+------------+------------------+------+-----+---------------------+----------------+ 
| id   | int(10) unsigned | NO | PRI | NULL    | auto_increment | 
| product_id | int(11)   | NO |  | NULL    |    | 
| term_id | int(11)   | NO |  | NULL    |    | 
| created_at | timestamp  | NO |  | 0000-00-00 00:00:00 |    | 
| updated_at | timestamp  | NO |  | 0000-00-00 00:00:00 |    | 
+------------+------------------+------+-----+---------------------+----------------+ 

我已經創建模型產品,分類和術語是這樣的:

<?php 

class Product extends Eloquent { 

    public function terms() 
    { 
     return $this->belongsToMany('Term', 'product_terms', 'product_id', 'term_id'); 
    } 

} 

<?php 

class Taxonomy extends Eloquent { 

    public function terms() 
    { 
     return $this->hasMany('Term'); 
    } 

} 

<?php 

class Term extends Eloquent { 

    public function taxonomy() 
    { 
     return $this->belongsTo('Taxonomy'); 
    } 

} 

現在我想做下面的事情,我想要得到所有的產品,循環播放,並顯示名稱,說明和價格。那麼,簡單的$products = Product::all();就是我所需要的。 然後,當我循環播放產品時,我知道我可以做類似$product->terms的所有條款。但是,如何得到一個給定的分類學術語。所以例如這樣的事情:

<?php 
echo $product->term('brand'); // Where 'brand' is the taxonomy name of the term I want to get 
echo $product->term('color'); // Where 'color' is the taxonomy name of the term I want to get 

希望有人能幫助我。

回答

3

好吧,我認爲我明白你的設置。鑑於你所說的關係,我想你會定義你的模型如下:

<?php 

class Product extends Eloquent { 

    public function terms() 
    { 
     return $this->belongsToMany('Term')->withTimestamps(); 
    } 

} 

<?php 

class Taxonomy extends Eloquent { 

    public function terms() 
    { 
     return $this->hasMany('Term'); 
    } 

} 

<?php 

class Term extends Eloquent { 

    public function taxonomy() 
    { 
     return $this->belongsTo('Taxonomy'); 
    } 

    public function products() 
    { 
     return $this->belongsToMany('Product')->withTimestamps(); 
    } 

} 

由於您的支點被命名爲正確id'd你不應該有指定的belongsToMany關係該信息,但如果你想要時間戳工作,你需要withTimestamps。

編輯:

如果你可以通過ID而不是塞看起來分類了,這將工作:

$products = Product::all(); 
foreach($products as $product) 
{ 
    $term = $product->terms()->with('taxonomy')->where('taxonomy_id', '=', 2)->first(); 
    echo $term->name; 
} 

在上述情形你可以只存儲分類塞在術語表因爲它應該是一個獨特的關鍵。否則,你可以看看在條款並尋找您想要的與分類:

$products = Product::all(); 
foreach($products as $product) 
{ 
    $terms = $product->terms()->with('taxonomy')->get(); 
    foreach($terms as $term) 
    { 
     if($term->taxonomy->slug == 'brand') 
     { 
      echo $term->name."<br />"; 
     } 
    } 
} 

編輯完

我不得不承認,我有點失落,但因爲你的最後兩個表看起來完全一樣,我想知道爲什麼你把產品與產品聯繫在一起而不是分類。我懷疑我的解決方案不適用於你關聯事物的方式,但如果你將分類法與產品相關聯,那麼它將工作得很好。

<?php 
$product->taxonomy()->where('slug', '=', 'brand')->term; 
+0

謝謝你的回答,我今天晚些時候會看看它。但只是一個快速清理,爲什麼我有關於產品的條款。分類只是一組術語的名稱。你有像'紅色,藍色,綠色,黃色'的術語和這些術語的分類是'顏色'。對於一個產品我想獲得它的「條款」。不是它的分類法。因爲通常情況下,產品將具有所有分類法。我對它具有的給定分類學的哪些術語感興趣。 「什麼顏色的產品」,「什麼品牌的產品」 – Crinsane 2013-04-05 17:39:40

+0

噢,順便說一句,最後兩個表確實相同,但這是一個錯誤,馬上更新它。 – Crinsane 2013-04-05 17:42:50

+0

我無法得到這個工作。獲取此錯誤: 調用未定義的方法Illuminate \ Database \ Query \ Builder :: taxonomy()' – Crinsane 2013-04-05 21:30:57