4

私は 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

誰かが私を助けてくれることを願っています。

4

1 に答える 1

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 が正しく設定されているため、この情報を belongsToMany 関係で指定する必要はありませんが、タイムスタンプを機能させたい場合は withTimestamps が必要です。

編集:

slug の代わりに 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 />";
        }
    }
}

編集終了

ただし、最後の 2 つのテーブルはまったく同じように見えるため、少し迷っていることを認めなければなりません。分類法を製品ではなく、用語を製品に関連付ける理由が不思議です。私の解決策は、あなたが物事を関連付けている方法では機能しないと思われますが、代わりに分類法を製品に関連付ければ、問題なく機能します.

<?php
$product->taxonomy()->where('slug', '=', 'brand')->term;
于 2013-04-05T14:20:19.090 に答える