小编gin*_*yan的帖子

Laravel Eloquent 模型继承

我是一个尝试使用 Laravel 5.5 和 Eloquent 模型构建应用程序的新手。

我有两个类:(1) Customer 和(2) 扩展 Customer 的VIPCustomer

您可以立即告诉 VIPCustomer 包含客户拥有的所有属性以及其他额外属性。

需要明确的是,客户可能不是VIP,而VIP必须是客户;客户可以在他第一次购物时立即选择成为 VIP。

因此,我试图在数据库中做这样的事情:

顾客:

+------------------------+
|id|name|gender|join_date|
+------------------------+
Run Code Online (Sandbox Code Playgroud)

VIP客户:

+----------------------------------+
|customer_id|valid_until|type|point|
+----------------------------------+
(customer_id is a foriegn key referencing Customer.id)
Run Code Online (Sandbox Code Playgroud)

因此,在模型 php 文件中:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{

}
Run Code Online (Sandbox Code Playgroud)

.

namespace App;

use Illuminate\Database\Eloquent\Model;

class VIPCustomer extends Customer
{
    public $incrementing = false;
}
Run Code Online (Sandbox Code Playgroud)

就这样?我看到有人说我应该使用多态关系,但我不明白这是什么意思。

另外,是否可以像这样实例化一个新的VIP客户?

$customer = new VIPCustomer;
$customer->name   = 'Alice';
$customer->gender = 'F';
$customer->type   = 'gold';
$customer->point …
Run Code Online (Sandbox Code Playgroud)

php inheritance model laravel eloquent

5
推荐指数
1
解决办法
4210
查看次数

标签 统计

eloquent ×1

inheritance ×1

laravel ×1

model ×1

php ×1