Closed tomasvanrijsse closed 6 years ago
Your last table is what the table would look like if you use this package. Yes this would be another Model but your implementation wouldn't really change:
foreach($product->getTranslation() as $value) {
echo $value->property->name . ': ' . $value->value;
}
This will output: en
Color: red Wheel size: 23 inch
nl
Kleur: rood Wielgrootte: 58,4 cm
In this case I've also added the translatable trait to the properties
model itself.
@tomasvanrijsse , @Gummibeer you can use using()
over the belongsToMany()
relation - see https://github.com/dimsav/laravel-translatable/issues/303#issuecomment-417402510
Assuming your models as the following:
class Product extends \Illuminate\Database\Eloquent\Model
{
public function properties()
{
return $this
->belongsToMany(Property::class)
->withPivot(['product_id', 'property_id'])
->using(ProductProperties::class);
}
}
class Property extends \Illuminate\Database\Eloquent\Model
{
}
class ProductProperties extends \Illuminate\Database\Eloquent\Relations\Pivot
{
use Translatable;
protected $fillable = ['product_id', 'property_id'];
protected $translatedAttributes = ['value']
}
class ProductPropertyTranslation extends \Illuminate\Database\Eloquent\Model
{
protected $fillable = ['value'];
}
So then you can normally call the translated value column as the following:
app()->setLocale('en');
$product = Product::with('properties')->find($id);
foreach ($product->properties as $property){
echo $property->value;
}
https://laravel.com/docs/5.6/eloquent-relationships#many-to-many Defining Custom Intermediate Table Models
I've read issue #303 and was thinking about an approach to tackle pivot values. We have implemented the Entity-Attribute-Value database design for products and their properties. Our simplified schema looks like this
Products Entity
Properties Attribute
Product Properties Value
Using the pivot table as a many-to-many relation gives us a syntax like
To us the syntax in that foreach-loop makes sense. Using a separate model for the pivot table however would force awkward syntax like
$product->value->property
. Therefore we would like to keep the pivot functionality as it is.Ideally the pivot table would look like this
Product Properties Value
Do you concur that this schema would be a good solution? How should laravel-translatable be extended to make this scenario work?