Reputation: 595
I have created a global variable in my CartController for the quantity of an item, here is the declaration:
class CartController extends Controller
{
// defining global quantity variable
private $quantity = 1;
Then in my index()
function on the same controller, where I return the view, I pass through the quantity
variable like so:
public function index()
{
$this->quantity;
return view('cart.cart', ['quantity' => $this]);
}
But when I call it in the cart.blade.php
file like this:
<div class="display-quantity">
{{-- Shows Quantity --}}
<span class="item-quantity" id="item-quantity">{{ $quantity }}</span>
</div>
It gives me the following error:
TypeError
htmlspecialchars(): Argument #1 ($string) must be of type string, App\Http\Controllers\CartController given (View: /Users/rosscurrie/mobile-mastery-latest/resources/views/cart/cart.blade.php)
I think I need to convert it to a string but I need to work with it as an integer so how do I get around this? Thanks!
Upvotes: 1
Views: 1130
Reputation: 1833
It look like you're passing in $this
as the quantity
.
Try changing your controller's index()
code to something like:
public function index()
{
$myQuantity = $this->quantity;
return view('cart.cart', ['quantity' => $myQuantity]);
}
It looks like the confusion here is with the ->
operator which in php is used to call on object's method or access an object's property the same way .
works in Javascript. This statement $this->quantity;
accesses the quantity
property of $this
, so to use it - you need to assign it to a variable (or use it directly). This would have also worked:
public function index()
{
return view('cart.cart', ['quantity' => $this->quantity]);
}
You can always do things like dd($this->quantity);
to ensure you are working with the correct information.
Upvotes: 1