Reputation: 39
I am working on adding the product values into the cart and i am using darryldecode Laravel shopping cart.
When I try to get the image and total price from the attribute array, I get the error Undefined property: stdClass::$total
.
Here is my Controller:
public function cart(Request $request , $id)
{
// return $request;
$cart = Cart::add([
'id' => $request->id,
"name" => $request->name,
"crm" => $request->sku,
"quantity" => $request->qty,
"price" => $request->price,
"attributes" => array(["image" => $request->image] , "total" => $request->price * $request->qty)
]);
if($cart)
{
return redirect()->route('cart');
}
}
Here is the Cart Controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Cart;
class CartController extends Controller
{
public function index()
{
// return Cart::getContent();
return View('demo', [ 'cart' => Cart::getContent()]);
}
}
Here is the view where I try to print the image and total
@foreach($cart as $product)
Name:<td>{{ $product->name}}</td>
Price:<td>{{ $product->price}}</td>
Quantity:<td>{{ $product->qty}}</td>
Attributes:<td>{{ $product->attributes}}</td>
@foreach(json_decode($product->attributes) as $details)
Image:<td>{{ $details->image}}</td>
Total:<td>{{ $details->total}}</td>
{{ $details}}
@endforeach
@endforeach
Upvotes: 1
Views: 1051
Reputation: 1270
You have to add both attributes into one array when you want get into view (blade) file.
Use below code:
$cart = Cart::add([
'id' => $request->id,
"name" => $request->name,
"crm" => $request->sku,
"quantity" => $request->qty,
"price" => $request->price,
"attributes" => array(["image" => $request->image, "total" => $request->price * $request->qty])
]);
Upvotes: 0
Reputation: 4774
You have a typo in this line:
"attributes" => array(["image" => $request->image] , "total" => $request->price * $request->qty)
Rather do this:
"attributes" => ["image" => $request->image, "total" => $request->price * $request->qty]
Upvotes: 1