dtdd25012
dtdd25012
2018-05-18 22:52

Eloquent如果我在一个型号上有两个“一对多”型号,我该如何存储数据?

  • php
  • database-design
已采纳

Ok, i have the following ER Diagram that shows my relations in DB: enter image description here I am willing to generate a User when the Order is made. The Order can contain many Products, also the Orders are grouped by their transaction_id, so i can include all the products in the same order for a User. I am trying to save them all but i am getting Field 'transaction_id' doesn't have a default value. I've managed to save the user, also to link the order with the product and the user but i still can't figure out how to save transaction_id linked with order. These are my migrations:

Schema::create('orders', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned()->index()->nullable();
            $table->foreign('user_id')->references('id')->on('users');
            $table->integer('transaction_id')->unsigned();
            $table->foreign('transaction_id')->references('id')->on('transactions');
            $table->timestamps();

Schema::create('order_product', function (Blueprint $table) {
            $table->integer('order_id')->unsigned()->index();
            $table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
            $table->integer('product_id')->unsigned()->index();
            $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
            $table->integer('quantity')->unsigned();
            $table->timestamps();
        });
Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->float('price',8,2);
            $table->timestamps();
        });
Schema::create('transactions', function (Blueprint $table) {
            $table->increments('id');
            $table->enum('status',['open','closed']);
            $table->timestamps();
        });

My models:

User Model:

public function orders() {return $this->hasMany(Order::class);}

Order Model:

public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function products()
    {
        return $this->belongsToMany(Product::class)
        ->withPivot('quantity')
        ->withTimestamps();
    }

    public function transaction()
    {
        return $this->belongsTo(Transaction::class);
    }

Transaction Model:

public function orders() {return $this->hasMany(Order::class);}

This is how i'm trying to save them:

$user->save();
$transaction->save();
$order = new Order();
$order->user_id = $user->id;
$user->orders()->save($order);
$transaction->orders()->save($order);
$order->products()->attach($cartItem->id, ['quantity' => $cartItem->qty]);

P.S. Sorry for the long post, i am out of ideeas.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douliang6563 douliang6563 3年前

    Use this:

    $order = new Order();
    $order->user_id = $user->id;
    $order->transaction_id = $transaction->id;
    $order->save();
    
    点赞 评论 复制链接分享