问题描述
有可能代替<input name="...."/>
而不是从div
或span
等获得我的值吗?
Is that possible instead of using <input name="...."/>
I get my value from div
or span
etc. ?
目前,我是根据产品ID直接从数据库中获取数据的,但是由于我的产品可能会有折扣,折扣会显示与数据库中保存的价格不同的价格,因此我无法将其显示为input
,因此我需要通过通过div
或span
等
currently I get my data directly from database, base on product id, but as my product might have discount that will show different price with what is saved in my database and I cannot show it as input
so i need to pass it through div
or span
etc.
这是我的代码:
public function addingItem(Request $request, $id)
{
$product = Product::findOrFail($id);
Cart::add(array(
'id' => $product->id,
'name' => $product->title,
'price' => $product->price, // this comes directly from products table
));
}
使用我的代码,我总是会得到45.325
,但是在折扣期间我需要得到35.325
.
with my code i always will get 45.325
but I need to get 35.325
during discount time.
这就是为什么我需要将其传递通过div
并且不能在此处使用input
的原因.
That's why i need to pass it through div
and cannot use input
here.
有什么主意吗?
推荐答案
已解决
好吧,我决定将代码带到控制器,而不是使用ajax或其他方式,这是我的方法:
SOLVED
well I decided to bring my codes to controller instead of using ajax or other ways, here is how I've done it:
$discounts = Discount::all();
$mytime = Carbon::now();
//get discounted price or normal price of product
$price = $product->discounts;
if($price->count() > 0 ) {
foreach($discounts as $disc){
if($disc->value_to >= $mytime) {
$price = $product->price - $disc->amount;
}
}
}else{
$price = $product->price;
}
希望它可以帮助其他人.
Hope it help others.
这篇关于将数据从div发送到Laravel中的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!