Laravel 5 checkout表单将外键插入到事务表中

时间:2015-09-07 18:43:56

标签: foreign-keys

现在我正在使用laravel 5.0和crinsane laravel包研究电子商务网站的流程。

我设置了2个表

哪个是交易和订单表

关系是订单有很多交易(1交易1种商品),交易属于订单。

因此,在事务中,存在引用订单表id的外键order_id。

在路线中我设置了route :: post(' checkout',' OrderController @ checkoutpost');

public function checkoutpost()
{
// Get input from checkout forms 
    $input = Request::all();
// Insert forms data into Order table 
    Order::create($input);

// Retrieve the session data and inserting into Transaction table

    $formid = str_random();
    $cart_content = Cart::content();

    foreach ($cart_content as $cart) {

        $transaction = new Transaction();

        $products = Product::find($cart->id);

        $transaction->product_id = $cart->id;
        $transaction->form_id = $formid;
        $transaction->qty = $cart->qty;
        $transaction->total_price = $cart->price * $cart->qty;

    // Here is the problem , how to assign this transaction>order_id into our "id" that just inserted earlier ..    
        $transaction->order_id = $orders;

        $transaction->save();


        Cart::destroy();
        return redirect('product/checkout');
    }
}

问题是如何使用我们之前插入的数据的id分配order_id?

非常感谢任何反馈,谢谢

2 个答案:

答案 0 :(得分:1)

首先,在创建订单时,您需要指定返回值:

// An instance of Order is returned, so the id is accessible.
$order = Order::create($input);

然后你可以使用:

// Remember to make 'id' a fillable field on the Order model if you want to do it this way.
$transaction->order_id = $order->id;

答案 1 :(得分:-1)

你是否尝试过这款AvoRed Laravel电子商务,如果你愿意的话,它可以为Laravel提供几乎功能齐全的电子商务尝试,如果你有任何反馈,请告诉我反馈。

AvoRed An Laravel E commerce