Relation Table product et table discount

Par uknowcreation, il y a 10 ans


Bonjour,
Je suis actuellement surt un petit test de projet ecommerce avec laravel 4.2 et j'ai une table product avec

$table->increments('id'); $table->string('name'); $table->mediumText('description'); $table->integer('stock'); $table->integer('rating'); $table->float('price'); $table->integer('discount_id')->nullable(); $table->integer('category_id');

et une table reduction (Discount)

$table->increments('id'); $table->string('name'); $table->decimal('discount'); $table->integer('parent_id')->nullable(); $table->timestamps();

Seulement soucis je cherche à recuperer mon pourcentage de remise via mon controller Products
Voici Mon model Product

class Product extends Eloquent { protected $table = "product"; protected $guarded = ["id"]; protected $softDelete = true; public function discount(){ return $this->hasOne('Discount'); } }

En gros j'aimerais Afficher donc mon prix du produits mais avec ma foreign key discount_id dans ma table product je souhaite récupérer donc aprés le taux de remise pour afficher mon prix avant et et aprés remise.
Voilà
Merci bien

3 réponses

Alexandre #lbac, il y a 10 ans

Pour un tel projet je ne peux que te conseiller de partir sur Laravel 5.1 (mis à jour pendant 3 ans) contrairement à 4.2 qui est désormais obsolète si je ne m'abuse.

Il te manque également la relation belongsTo dans ton Discount Model pour pouvoir faire $product->discount() (la méthode discount sera dans product model)

uknowcreation, il y a 10 ans

Merci encore betaweb mais j'ai aussi gerer avec les egg loader