Reputation: 39
I have this function on Cart Model
//Cart.php
<?php
namespace App;
class Cart
{
public $items= null;
public $totalQty=0;
public $totalPrice=0;
public function __construct($oldCart){
if($oldCart){
$this->items= $oldCart->items;
$this->totalQty= $oldCart->totalQty;
$this->totalPrice= $oldCart->totalPrice;
}
}
public function addCart($item, $id){
$storedItem=['qty'=>0, 'item'=>$item, 'price'=>$item->price];
if($this->items){
if(array_key_exists($id, $this->items)){
$storedItem= $this->items[$id];
}
}
$storedItem['qty']++;
$storedItem['price'] *= $storedItem['qty'];
$this->items[$id]= $storedItem;
$this->totalQty++;
$this->totalPrice +=$storedItem['price'];
}
}
and this is my function on my controller
public function addToCart(Request $request, $id){
$pork=Pork::where('did', $id)->get();
$oldCart=Session::has('cart') ? Session::get('cart') : null;
$cart= new Cart($oldCart);
$cart->addCart($pork, $id);
$request->session()->put('cart', $cart);
return redirect()->route('user.index', compact('pork'));
}
I am trying to get the price so I could calculate it. The code is working well, but when i tried adding 'price'=>$item->price on the storedItem array. It is giving error which is (1/1) Exception Property [price] does not exist on this collection instance. (Laravel 5.4)
$storedItem=['qty'=>0, 'item'=>$item, 'price'=>$item->price];
could anyone help me? what should i do with this?
here is my blade view
@if(isset(Session::get('cart')->items))
@foreach(Session::get('cart')->items as $crt)
@foreach($crt['item'] as $pork)
<dl class="dl-horizontal">
<div id="cartdiv">
<dt>
<div>
<input type="number" min="1" value="{{$crt['qty']}}">
</dt>
<dd>
<label>
<b> {{$pork['pork_name']}}</b>
</label>
</dd>
<dt>
<label"> Price: <b id="bprice" name="price">{{$pork['basePrice']}}</b></label>
</dt>
<dd>
<label>Total Amount: 200.00</label>
</dd>
</dt>
</div>
</dl>
@endforeach
@endforeach
@endif
Upvotes: 0
Views: 307
Reputation: 779
get()
will return a collection even if only 1 result found, you can use first()
to get the first result.
Just change
$pork=Pork::where('did', $id)->get();
to $pork=Pork::where('did', $id)->first();
view file should changed to
@if(isset(Session::get('cart')->items))
@foreach(Session::get('cart')->items as $crt)
<dl class="dl-horizontal">
<div id="cartdiv">
<dt>
<div>
<input type="number" min="1" value="{{$crt['qty']}}">
</dt>
<dd>
<label>
<b> {{$crt['item']->name}}</b>
</label>
</dd>
<dt>
<label"> Price: <b id="bprice" name="price">{{$crt['item']->price}}</b></label>
</dt>
<dd>
<label>Total Amount: 200.00</label>
</dd>
</dt>
</div>
</dl>
@endforeach
@endif
Upvotes: 1
Reputation: 39
@if(isset(Session::get('cart')->items))
@foreach(Session::get('cart')->items as $crt)
@foreach($crt['item'] as $pork)
<dl class="dl-horizontal">
<div id="cartdiv">
<dt>
<div>
<input type="number" min="1" value="{{$crt['qty']}}">
</dt>
<dd>
<label>
<b> {{$pork['pork_name']}}</b>
</label>
</dd>
<dt>
<label"> Price: <b id="bprice" name="price">{{$pork['basePrice']}}</b></label>
</dt>
<dd>
<label>Total Amount: 200.00</label>
</dd>
</dt>
</div>
</dl>
@endforeach
@endforeach
@endif
That's my blade view
Upvotes: 0
Reputation: 6045
As @wanghanlin has stated - you need to pick the first
record from the collection
- also Session::get()
has second argument, which by default is null
so you could refactor as:
public function addToCart(Request $request, $id)
{
$pork = Pork::where('did', $id)->get()->first() ?? new Pork;
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);
$cart->addCart($pork, $id);
$request->session()->put('cart', $cart);
return redirect()->route('user.index', compact('pork'));
}
Upvotes: 1