管理面板中的订单和订单状态表之间的关系

时间:2019-10-03 18:33:51

标签: php laravel laravel-5.8 laravel-6

我正在ordersorder_status表之间创建外键关系。当我从客户那里收到新订单并发货时,我将订单状态设置为已收到该客户的发货。我设置了已发货和已接收状态,然后该订单将开始显示在“订单历史记录”页面上。我这样做正确吗?

class CreateOrdersTable extends Migration
{
    public function up()
    {
        Schema::create('orders', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->text('customer_name');
            $table->string('customer_email');
            $table->string('customer_contact');
            $table->bigInteger('order_status_id')->unsigned();
            $table->string('product_names');
            $table->string('products_quantity');
            $table->text('customer_address');
            $table->timestamps();
    });
}

这是order_status表:

class CreateOrderStatusTable extends Migration
{
    public function up()
    {
        Schema::create('order_status', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('status')->nullable();
            $table->timestamps();
        });
    }
}

1 个答案:

答案 0 :(得分:0)

从外观上看,一个Order可以具有多个OrderStatuses。为了在Eloquent中简化此操作(此处未介绍),您的order_status表需要另外一列引用orders表。

将此添加到order_status迁移中:

$table->bigInteger('order_id')->unsigned()->index();
$table->foreign('order_id')->references('id')->on('orders');