我已经制作了更新表单,以便在2个条件下获取数据
他们应该并排工作,相互打断, LOGIC 是:
quantity
,请将其更新coupon
,请将其应用quantity and add coupon
,请同时更新。works
,如果我更改数量。doesn't work
优惠券更新。doesn't work
,两个输入在一起。2 sessions alert
。PS:我的问题是参考我的
if
陈述。 (基本上我需要帮助来修复我的if's
)
public function update(Request $request, $id)
{
//list of coupons
$coupons = Coupon::all();
//get current time
$mytime = Carbon::now();
$qty = $request->input('quantity');
$productId = $request->proId;
$products = Product::where('id', $productId)->firstOrFail();
$stock = $products->stock;
//coupons
$couponcode = $request->input('coupon');
$catId = Coupon::where('category_id', $products->category_id)
->where('value_to', '>=', $mytime)
->when($couponcode, function ($query) use ($couponcode) {
return $query->where('title', $couponcode);
})
->first();
if($catId == $couponcode){ //first if
$coupon = new \Darryldecode\Cart\CartCondition(array(
'name' => $catId->title,
'type' => 'coupon',
'target' => 'item',
'value' => -$catId->amount,
));
}else{
Session::flash('danger', 'Your request cannot be handle, please try again!');
}
//end coupons
if($qty<$stock && empty($couponcode)){ //second if
Cart::update($id, array(
'quantity' => array(
'relative' => false,
'value' => $request->input('quantity'),
),
));
Session::flash('success', 'Cart is updated.');
return redirect()->route('cart.index');
}elseif(!empty($couponcode) && $catId == $couponcode){ //third if
Cart::update($id, array(
'conditions' => $coupon,
));
Session::flash('success', 'Coupon applied successfully.');
return redirect()->route('cart.index');
}else{ // fourth if
Session::flash('danger', 'Your request cannot be handle, please try again!');
return redirect()->route('cart.index');
}
}
任何人都可以提供帮助吗?
答案 0 :(得分:0)
当你创建$catId
时,你会得到一个雄辩的模型,因为你使用first()
。
$ catId =优惠券::其中(&#39; category_id&#39;,$ products-&gt; category_id) - &gt; where(&#39; value_to&#39;,&#39;&gt; =&#39;,$ mytime) - &gt; when($ couponcode,function($ query)use($ couponcode){ 返回$ query-&gt; where(&#39; title&#39;,$ couponcode); }) - &gt;首先();
然后你比较它:
if($catId == $couponcode)
永远不会是真的
解决方案是从查询中正确获取所需的值:
...->first()->column_name;
答案 1 :(得分:0)
您应该在条件 s 中使用优惠券代码($catId->title
或$catId->category_id
或其他任何内容)的列名:< / p>
if($catId->title == $couponcode){ //first if
$coupon = new \Darryldecode\Cart\CartCondition(array(
'name' => $catId->title,
'type' => 'coupon',
'target' => 'item',
'value' => $catId->amount,
));
}else{
Session::flash('danger', 'Your request cannot be handle, please try again!');
}
同样在:
elseif(!empty($couponcode) && $catId->title == $couponcode){ //third if
<强>更新强>
您应该将if块重新排列为:
1。如果同时给出(数量和代码)
嵌套&gt; 如果数量小于库存且代码正确update the cart
2。如果仅提供数量update quantity
3。如果只是优惠券apply coupon
4. 如果没有给出do nothing
// quantity
$qty = $request->input('quantity');
$products = Product::findOrFail($request->proId);
$stock = $products->stock;
// coupons
$couponcode = $request->input('coupon');
$catId = Coupon::where('category_id', $products->category_id)
->where('value_to', '>=', $mytime)
->when($couponcode, function ($query) use ($couponcode) {
return $query->where('title', $couponcode);
})
->first();
if (!empty($qty) && !empty($couponcode)) { // 1. if both (quantity and couponcode) are given
if ($qty < $stock && (!is_null($catId) && $catId->title == $couponcode)) { // if quantity < stock AND coupon-code is correct
$coupon = new \Darryldecode\Cart\CartCondition(array(
'name' => $catId->title,
'type' => 'coupon',
'target' => 'item',
'value' => $catId->amount,
));
Cart::update($id, array(
'quantity' => array(
'relative' => false,
'value' => $qty,
),
'conditions' => $coupon,
));
Session::flash('success', 'Cart updated.');
return redirect()->route('cart.index');
} elseif ($qty > $stock) {
Session::flash('danger', 'quantity not available!');
return redirect()->route('cart.index');
} else {
Session::flash('danger', 'invalid coupon code!');
return redirect()->route('cart.index');
}
} elseif (!empty($qty)) { // 2. if just quantity is given
if ($qty < $stock) {
Cart::update($id, array(
'quantity' => array(
'relative' => false,
'value' => $qty,
),
));
Session::flash('success', 'Cart updated.');
return redirect()->route('cart.index');
} else {
Session::flash('danger', 'quantity not available!');
return redirect()->route('cart.index');
}
} elseif (!empty($couponcode)) { // 3. if just couponcode is given
if (!is_null($catId) && $catId->title == $couponcode) {
$coupon = new \Darryldecode\Cart\CartCondition(array(
'name' => $catId->title,
'type' => 'coupon',
'target' => 'item',
'value' => $catId->amount,
));
Cart::update($id, array(
'conditions' => $coupon,
));
Session::flash('success', 'Coupon applied successfully.');
return redirect()->route('cart.index');
} else {
Session::flash('danger', 'invalid coupon code!');
return redirect()->route('cart.index');
}
} else{ // 1. if nothing is given
Session::flash('danger', 'Your request cannot be handle, please try again!');
return redirect()->route('cart.index');
}