Note that there are some explanatory texts on larger screens.

plurals
  1. POLaravel Eloquent Many to Many how to
    primarykey
    data
    text
    <p>I'm trying to learn Laravel and right now I'm experimenting with Eloquent. I've got the following case that I can't solve right now:</p> <p>Let's say I'm creating a webshop, so I have products (Product 1, Product 2, etc):</p> <pre><code>+-------------+------------------+------+-----+---------------------+----------------+ | 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 | | +-------------+------------------+------+-----+---------------------+----------------+ </code></pre> <p>Those products are sorted into Taxonomies (Brand, Color, Department):</p> <pre><code>+-------------+------------------+------+-----+---------------------+----------------+ | 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 | | +-------------+------------------+------+-----+---------------------+----------------+ </code></pre> <p>These taxonomies have terms (Nike, Adidas, Red, Green, Boys, Girls):</p> <pre><code>+-------------+------------------+------+-----+---------------------+----------------+ | 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 | | +-------------+------------------+------+-----+---------------------+----------------+ </code></pre> <p>And last I have a pivot table to connect the terms to the products:</p> <pre><code>+------------+------------------+------+-----+---------------------+----------------+ | 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 | | +------------+------------------+------+-----+---------------------+----------------+ </code></pre> <p>I've created models for Product, Taxonomy and Term like this:</p> <pre><code>&lt;?php class Product extends Eloquent { public function terms() { return $this-&gt;belongsToMany('Term', 'product_terms', 'product_id', 'term_id'); } } &lt;?php class Taxonomy extends Eloquent { public function terms() { return $this-&gt;hasMany('Term'); } } &lt;?php class Term extends Eloquent { public function taxonomy() { return $this-&gt;belongsTo('Taxonomy'); } } </code></pre> <p>Now I want to do the following, I want to get all the products, loop over them and show something like the name, description and price. Well, a simple <code>$products = Product::all();</code> is all I need. Then, when I loop over the products, I know I can do something like <code>$product-&gt;terms</code> to get all the terms. But how should a get the term of a given taxonomy. So for example something like this:</p> <pre><code>&lt;?php echo $product-&gt;term('brand'); // Where 'brand' is the taxonomy name of the term I want to get echo $product-&gt;term('color'); // Where 'color' is the taxonomy name of the term I want to get </code></pre> <p>Hope someone can help me. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload