lundi 29 février 2016

Relationships of models (Laravel 5.2)

My tables (Mysql DB):

// Stores Table

CREATE TABLE IF NOT EXISTS `app_beta`.`stores` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NOT NULL,
 PRIMARY KEY (`id`))

// Items Table

 CREATE TABLE IF NOT EXISTS `app_beta`.`items` (
 `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
 `user_id` INT UNSIGNED NOT NULL,
 `title` TEXT NOT NULL,
 `content` LONGTEXT NULL,
  PRIMARY KEY (`id`),
  CONSTRAINT `fk_items_user_id`
  FOREIGN KEY (`user_id`)
  REFERENCES `app_beta`.`users` (`id`))

// Products Table

CREATE TABLE IF NOT EXISTS `app_beta`.`products` (
`id` INT UNSIGNED NOT NULL,
`reviews` DECIMAL(7,1) NOT NULL,
 PRIMARY KEY (`id`),
 CONSTRAINT `fk_products_id`
 FOREIGN KEY (`id`)
REFERENCES `app_beta`.`items` (`id`))

// Product_Store Table

CREATE TABLE IF NOT EXISTS `app_beta`.`products_stores` (
`product_id` INT UNSIGNED NOT NULL,
`store_id` INT UNSIGNED NOT NULL,
`price` DECIMAL(7,2) NOT NULL,
`url` VARCHAR(255) NOT NULL,
 CONSTRAINT `fk_products_store_product_id`
 FOREIGN KEY (`product_id`)
 REFERENCES `app_beta`.`products` (`id`),
 CONSTRAINT `fk_products_stores_store_id`
 FOREIGN KEY (`store_id`)
 REFERENCES `app_beta`.`stores` (`id`))

// Offers Table

CREATE TABLE IF NOT EXISTS `app_beta`.`offers` (
`id` INT UNSIGNED NOT NULL,
`store_id` INT UNSIGNED NOT NULL,
`price` DECIMAL(7,2) NULL,
`url` VARCHAR(255) NOT NULL,
`start_date` DATE NOT NULL,
`end_date` DATE NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `fk_offers_store_id`
FOREIGN KEY (`store_id`)
REFERENCES `app_beta`.`stores` (`id`),
CONSTRAINT `fk_offers_id`
FOREIGN KEY (`id`)
REFERENCES `app_beta`.`items` (`id`))

"Offers" and "Products" tables inherit from "items" table, simply beacause products can have many stores, by against "offers" can only have one store (please take a look of foreign keys to understand the relationships).

I have the 4 models on Laravel 5.2 (Store,Item,Product and Offer) and I want to make relationships between them. Please can anyone help me to do that!

Thanks.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1VNfYZF
via IFTTT

Aucun commentaire:

Enregistrer un commentaire