I am trying to create a simple shopping application using Laravel, I am using LaravelShoppingCart plugin to store items in the user session. When the user hits my store
method the order
and order_line
records should be stored.
I am currently getting this error with the code below:
Argument 1 passed to Illuminate\Database\Eloquent\Model::__construct() must be of the type array, object given, called in /home/vagrant/site/app/Http/Controllers/BasketController.php on line 130 and defined
Table schema:
// create orders table
Schema::create('orders', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned()->nullable();
$table->foreign('user_id')->references('id')->on('users');
$table->text('order_notes')->nullable();
$table->decimal('subtotal', 5, 2)->default(0);
$table->timestamps();
});
// create order lines table
Schema::create('order_lines', function (Blueprint $table) {
$table->increments('id');
$table->integer('order_id')->unsigned();
$table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
$table->integer('menu_item_id')->unsigned();
$table->foreign('menu_item_id')->references('id')->on('menu_item');
$table->decimal('sale_price', 5, 2);
$table->integer('quantity');
$table->timestamps();
});
Order Model (relationship):
/**
* Define relationship to OrderLines
* @return [type] [description]
*/
public function order_line()
{
return $this->hasMany('App\OrderLine');
}
OrderLine Model (relationship):
public function order()
{
return $this->belongsTo('App\Order');
}
Store Function:
public function store(CreateOrderGuestRequest $request)
{
$order = new Order;
$order->order_notes = $request->order_notes;
$order->save();
$order_lines = new Collection();
foreach (Cart::content() as $row) {
$order_lines->push([
'menu_item_id' => $row->id,
'quanity' => $row->qty,
'sale_price' => $row->price
]);
}
$order->order_line()->saveMany(new OrderLine($order_lines));
// redirect somewhere after
}