mysql - 模型的关系 (Laravel 5.2)

标签 mysql laravel model-view-controller laravel-5 eloquent

我的表(Mysql 数据库):

//存储表

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

//项目表

 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`))

//产品表

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 表

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`))

//报价表

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`))

添加。信息:

我的表已迁移。只是为了澄清......产品和报价继承自 items 表。如果未创建该项目,我将无法添加产品和优惠。

产品 可以具有标题、摘要、内容、类别等...与报价相同。

  • 该产品可以在 1-many 商店上销售
  • 优惠只能在 1-1 家商店。

如果我错了请告诉我!

** 拜托,我希望有人帮助我创建项目模型、产品和报价之间的关系。我可以使用多态关系吗? **

模型完成:

class Store extends Model
{
public function offers()
{
    return $this->hasMany('App\Offer');
}

public function products()
{
    return $this->hasMany('App\Product');
}
}

class Product extends Model
{
public function stores()
{
    return $this->belongsToMany('App\Store');
}
}

class Offer extends Model
{
public function store()
{
    return $this->belongsTo('App\Offer');
}
}

使用 php artisan tinker,一切正常!

namespace App

$user = new User
$store = new Store

$item = new Item
$item->id = 1
$item->user_id = 1
$item->title = 'test'
$item->content 'test'
$item->save();
true

$item2 = new Item
$item2->id = 2
....
true

$product1 = new Product
$product1->id = 1 (FK item->id)
$product1->reviews = 5
$product1->save()
true 

$offer1 = new Offer
$offer1->id = 2 (FK item->id)
$offer1->store_id = 1

...
true

稍后我将添加一个函数来将产品附加到一个或多个商店(products_stores 表)。

谢谢。

最佳答案

这就是我认为您可以有一个好的开始...

首先,您的模型和迁移可以处理所有这些。

关系有:Laravel 5.2 Relationship 有迁移:Laravel 5.2 Migration

所以你创建了你的迁移:

Schema::create('stores', function (Blueprint $table) {
    $table->bigIncrements('id')->unsigned();
    $table->string('name', 50);
    $table->timestamps();
});

Schema::create('items', function (Blueprint $table) {
    $table->bigIncrements('id')->unsigned();
    $table->bigInteger('user_id')->unsigned();
    $table->foreign('user_id')->references('id')->on('users');
    $table->text('title');
    $table->longText('content');
    $table->timestamps();
});

Schema::create('products', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->bigInteger('store_id')->unsigned();
    $table->foreign('store_id')->references('id')->on('stores');
    $table->decimal('reviews', 7,1);
    $table->timestamps();
});

Schema::create('offers', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->bigInteger('store_id')->unsigned();
    $table->foreign('store_id')->references('id')->on('stores');
    $table->bigInteger('item_id')->unsigned();
    $table->foreign('item_id')->references('id')->on('items');
    $table->decimal('price', 7,2);
    $table->string('url', 255);
    $table->dte('start_date');
    $table->dte('end_date');
    $table->timestamps();
});

因此,完成此操作后,您就可以将您的关系添加到您的模型中。这样你就不需要所有的“中间”表。当你使用 associate() 时,Laravel 会为你创建链接。这样你就可以做这样的事情: $offer->store()->name 来获取当前报价的商店名称。看一看:

进入Store的模型

public function products()
{
    return $this->hasMany(Product::class);
}

public function offers()
{
    return $this->hasMany(Offer::class);
}

进入Offer的模型

public function store()
{
    return $this->belongsTo(Store::class);
}

这样,您就创建了一对多关系。我说过,$offer->store() 将检索优惠的商店。 $store->offers()->get() 将检索商店的所有报价。

希望对您有所帮助。

编辑

我所说的只有一个问题。 n + 1 问题。所以就像它在那里解释(搜索谷歌“laravel n+1 问题”并选择 laracast 的链接)(不能把它作为链接,没有足够的声誉),当你像我说的那样调用时,脚本会做 2询问。当您使用 foreach() 循环时,它将有尽可能多的循环 +1 查询。我建议你这样做

$offers = Offer::with('store')->all();

这样你将只有 1 个查询,你仍然可以做

$offer->store;

无需执行另一个查询。

当您使用 $model = Model::with('something')->all(); 时,查询将从 2 个表中获取数据并将结果与​​数组一起返回到数组中。像这样:

offers {
    [0]:{a,b,c,d,e, store{a,b,c,d,e}}
    [1]:{a,b,c,d,e, store{a,b,c,d,e}}
    [2]:{a,b,c,d,e, store{a,b,c,d,e}}
    [3]:{a,b,c,d,e, store{a,b,c,d,e}}
}

您可以使用相反的方法:

$stores = Store::with('offers')->all();

所以你可以使用:

$store->offers[i]->somthing;

因为数组看起来像这样:

stores {
    [0]:{a,b,c,d,e, offers{
                        [0]:{a,b,c,d,e}
                        [1]:{a,b,c,d,e}
                        [2]:{a,b,c,d,e}
                        [3]:{a,b,c,d,e}
                        }}
    [1]:{a,b,c,d,e, offers{
                        [0]:{a,b,c,d,e}
                        [1]:{a,b,c,d,e}
                        [2]:{a,b,c,d,e}
                        [3]:{a,b,c,d,e}
                        }}
    [2]:{a,b,c,d,e, offers{
                        [0]:{a,b,c,d,e}
                        [1]:{a,b,c,d,e}
                        [2]:{a,b,c,d,e}
                        [3]:{a,b,c,d,e}
                        }}
}

关于mysql - 模型的关系 (Laravel 5.2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35706556/

相关文章:

php - MySQL/Laravel 外键约束格式不正确

mysql - 在 HeidiSQL 中执行查询时与 only_full_group_by 相关的错误

laravel - 在laravel eloquent中一次插入多条记录

asp.net-mvc-3 - ASP.NET Web API 问题 - 授权/身份验证

php - 从 iOS 向 php Web 服务发送请求

mysql - mysql回滚后出现重复唯一键错误

php - 无法将 FORM 中的变量与 PHP 中的 MYSQL 记录匹配

mysql - 在 Laravel 4 中执行分组最大值

javascript - 自定义组件的关注点分离?

iphone - 需要帮助来理解以编程方式创建的 UIView 和 UIViewController