顺便说一下,我是编程的新手,我现在正在努力学习Laravel ..
所以基本上,我正在为定制披萨创建一个订购系统,我正在集成一个类似于推车的功能..
我想做的是每次点击"添加披萨"它将重定向到比萨饼创建页面,然后将其存储为特定的order_id。对于单个order_id,我可以根据需要添加多个比萨饼,直到用户决定提交订单为止。
This is what I would like to happen
但每次我创建一个披萨..它总是将它存储到另一个order_id ..
这是我的代码的一部分:
这是显示订单中所有比萨饼的索引页面。
public function index()
{
//create new object orders
$orders = new Order;//where('user_id', '=', Auth::user()->id)->get();
return View::make('order.index')
->with('orders', $orders);
}
将披萨存储在订单中的代码..
public function store()
{
$user = User::find(Auth::user()->id);
$order= new Order;
$order->user()->associate($user);
$order->toAddress = 'Caimito St., Balite Drive, Brgy. Santiago';
$order->amount = 125.00;
$order->save();
$pizza = new Pizza;
$pizza->pizza_name='Jets Pizza';
$pizza->amount=0;
$quantity = Input::get('quantity');
$pizza->quantity = $quantity;
$pizza->save();
$base = Input::get('base');
$pizza->ingredients()->attach($base);
$cheese = Input::get('cheese');
$pizza->ingredients()->attach($cheese);
$meats = Input::get('meat');
if(sizeof($meats) != 0) {
foreach ($meats as $meat) {
$pizza->ingredients()->attach($meat);
}
}
$chilis = Input::get('chili');
if(sizeof($chilis) != 0) {
foreach ($chilis as $chill) {
$pizza->ingredients()->attach($chill);
}
}
$toppings = Input::get('toppings');
if(sizeof($toppings) != 0){
foreach ($toppings as $topping) {
$pizza->ingredients()->attach($topping);
}
}
$amount = 0;
foreach($pizza->ingredients as $ingr){
$amount = $amount + $ingr->price;
}
$total_amount;
$pizza->amount = $amount;
$pizza->save();
$order->pizzas()->attach($pizza);
// $order->pizzas->quantity = 5;
$order->save();
return Redirect::to('/order/index');
}
以下是我模特中关系的定义:
public function pizzas(){
// hasMany('ngalan sa model padulngan', 'ngalan sa junction table', 'name id sa gi-gikanan', 'name sa id sa padulngan nga junction table')
return $this->belongsToMany('Pizza','order_details','order_id','pizza_id');
}
public function orders(){
// belongsToMany('ngalan sa model', 'ngalan sa junction table' , 'name sa id sa padulngan nga junction table', 'name id sa gi-gikanan')
return $this->belongsToMany('Order','order_details', 'pizza_id' , 'order_id');
}
答案 0 :(得分:0)
据我所知,您的模型应该是:订购hasMany Pizza和Pizza所属的订单。
订单型号
public function pizzas(){
return $this->hasMany('Pizza');
}
比萨模特
public function order(){
return $this->belongsTo('Order');
}
在商店功能中,您可以创建单个订单和披萨。当您创建披萨时,您可以执行$ pizza-> order_id = $ order-> id(您必须先创建订单)。然后,您可以继续将配料添加到披萨中。我相信披萨不能属于许多订单。怎么会这样?