私は Laravel を学ぼうとしていますが、今は Eloquent を試しています。現在、解決できない次のケースがあります。
私がウェブショップを作成しているとしましょう。そのため、製品 (製品 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 | |
+-------------+------------------+------+-----+---------------------+----------------+
これらの分類には用語があります (Nike、Adidas、Red、Green、Boys、Girls)。
+-------------+------------------+------+-----+---------------------+----------------+
| 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 | |
+------------+------------------+------+-----+---------------------+----------------+
Product、Taxonomy、および Term のモデルを次のように作成しました。
<?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
誰かが私を助けてくれることを願っています。