我是一位新手,正在尝试使用Laravel 5.5和Eloquent模型构建应用程序。
我有两个类:(1)Customer和(2)VIPCustomer,VIPCustomer继承自Customer。
您可能会立即发现VIPCustomer包含了客户具有的所有属性以及其他额外属性。
为了明确起见,一个客户可能不是VIP,而VIP必须是客户;客户可以在第一次购物时立即选择成为VIP。
因此,我正在尝试在数据库中执行以下操作:
Customer:
+------------------------+
|id|name|gender|join_date|
+------------------------+
VIP客户:
+----------------------------------+
|customer_id|valid_until|type|point|
+----------------------------------+
(customer_id is a foriegn key referencing Customer.id)
相应地,在模型 PHP 文件中:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
}
.
namespace App;
use Illuminate\Database\Eloquent\Model;
class VIPCustomer extends Customer
{
public $incrementing = false;
}
And that's it? I saw there are others saying I should using polymorphic relationship but I don't understand what it means.
In addition, is it possible to do instantiate a new VIP Customer something like this?
$customer = new VIPCustomer;
$customer->name = 'Alice';
$customer->gender = 'F';
$customer->type = 'gold';
$customer->point = 0;
$customer->save();
On top of that, say when the VIP membership ends, is it possible to preserve that person as Customer? Because I'm afraid deleting that person will delete him from both Customer and VIPCustomer tables.
Thank you very much in advance.