Reputation: 111
I have the following tables
Look at this sqlfiddle for details http://sqlfiddle.com/#!2/eb1c73/24/0
Can I have something like this on my Product model to get all the attributes like I'm doing on the sqlfiddle query?
function attributes(){
return $this->hasManyThrough('Attributes','Variant');
}
THANKS!! My Models:
<?php
class Product extends \Eloquent {
protected $table = 'products';
public function user()
{
return $this->belongsTo('User');
}
public function variants()
{
return $this->hasMany('Variant');
}
public function attributes(){
return $this->hasManyThrough('Attribute','OptionVariant');
}
}
<?php
class Variant extends \Eloquent {
protected $table = 'variants';
public function product()
{
return $this->belongsTo('Product');
}
public function options()
{
return $this->belongsToMany('Option');
}
}
<?php
class Attribute extends \Eloquent {
protected $table = 'attributes';
public function options()
{
return $this->hasMany('Option');
}
}
<?php
class Option extends \Eloquent {
protected $table = 'options';
public function attribute()
{
return $this->belongsTo('Attribute');
}
public function variants()
{
return $this->belongsToMany('Variant');
}
}
<?php
class OptionVariant extends \Eloquent {
protected $table = 'option_variant';
}
Upvotes: 0
Views: 100
Reputation: 1411
If you want take all atributtes all time that you select the products:
In Product model:
$with = ['attributes'];
In Controller:
$products = $this->product->findAll();
return View::make('products.index', compact('products'));
In View:
@foreach($products as $product)
{{ $product->attributes->column1 }}
@endforeach
Upvotes: 1