Q:

Laravel 5 checkout form insert foreign key into transaction table

Right now i'm study a flow of ecommerce site using laravel 5.0 and crinsane laravel package .  
I have setup 2 tables 
Which is transactions and orders table 
The relations is orders has many transactions (1 transaction 1 type of item ) , and transactions belong to orders . 
So , in transactions there is foreign key order_id which references to order tables id .  
In routes I set 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');
    }
}

The problem is how to assign order_id with the id of data that we just insert earlier?  
Any feedback were really appreciated, thank you 

A:

Firstly, when creating the Order you need to assign the return value:
// An instance of Order is returned, so the id is accessible. 
$order = Order::create($input);

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

