Laravel在点击路线时会创建数据库输入吗?

时间:2016-03-16 17:26:28

标签: php mysql laravel laravel-routing laravel-5.2

我还在教自己拉拉。我在laravel做了一个订单,我有一个装满产品的桌子,我希望这样做,以便在下新订单并添加产品时。当我点击新订单时,它会自行订购它会生成一个新的订单号并将其保存到数据库中吗?

最好的方法是使用

制作订单表
Schema::create('westcoOrders', function (Blueprint $table){
        $table->increments('id');
        $table->string('orderNumber');
        $table->json('content');
        $table->tinyInteger('sent')->default(0);
        $table->tinyInteger('delivered')->default(0);
        $table->timestamps();
    } );

或者更好地拥有一个表多个表,所以我有一个像上面那个没有json条目,然后有一个像这样的westcoOrderItems表?

Schema::create('westcoOrderItems', function (Blueprint $table){
        $table->string('orderNumber');
        $table->string('quantity');
        $table->string('productName');
        $table->string('productCode');
        $table->string('price');
        $table->timestamps();
    } );
}

然后我将订单号链接到另一个表。或者这还有很长的路要走? 如果我这样做的话,当我点击新的订单路线时,我将不得不找到一个订购号码的方法吗?

我觉得我这样做是错误/漫长的?

1 个答案:

答案 0 :(得分:1)

在我看来,以下内容会更好:

// For orders
Schema::create('orders', function (Blueprint $table){
    $table->increments('id');
    $table->tinyInteger('is_delivered')->default(0);
    $table->tinyInteger('is_paid')->default(0);
    $table->timestamps();
});

// For ordered_items (one order may contain many products/ordered_items)
Schema::create('ordered_items', function (Blueprint $table){
    $table->increments('id')->unsigned();
    $table->integer('order_id')->unsigned(); // fk for order.id
    $table->string('quantity')->unsigned();
    $table->string('productName');
    $table->string('productCode');
    $table->decimal('price', 10, 2);
});

然后是模型,例如:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Order extends Model {

    public function items()
    {
        return $this->hasMany('Item::class');
    }
}

Item的{​​{1}}课程:

ordered_items

希望你明白了。这是最简单的一个。