0

I have a problem where a data cannot be displayed on blade but can be recognized on debug

This is The Controller

public function cart($cid)
{
    // $cart = DB::select("select*from carts where id = ?", [$cid]);
    $cart = Cart::find( $cid );
    // dd($cart);
    $pizza = Pizza::find( $cart->Pizzaid );
    // dd($pizza);
    return view('cart')->with('cart', $cart)->with('pizza', $pizza);
}

this is the "cart.blade.php"

@extends('layouts.app')

@section('content')
{{-- View Cart--}}

<div class="container">
    @foreach($cart as $c)
        <div class="card">
            <div class="card-body">
                <div class="row">
                    <div class="col-xl-3">
                        <img class="card-img-top" src="{{asset("assets/$pizza->Image")}}" alt="" style="object-fit:cover">{{-- taking the image of the pizza that the user bought --}}
                    </div>
                    <div class="col-xl-7">
                        <p class="font-weight-bold">{{ $pizza->Name }}</p>
                        <p>{{ $pizza->Price }}{{-- Price --}}</p>
                        <p>{{ $c->Quantity }}</p>
                        <div class="row">
                            <p class="col-4">Quantity: </p>
                            <input class="form-control col-7" type="text" placeholder="">
                        </div>
                        <p>{{ $pizza->Price * $c->Quantity }}</p>
                    </div>
                </div>
            </div>
        </div>
     @endforeach 
</div>

@endsection

this is the "dd(cart)"

App\Cart {#1078 ▼
  #connection: "mysql"
  #table: "carts"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:6 [▼
    "userid" => 2
    "Pizzaid" => 1
    "id" => 2
    "Quantity" => 1
    "created_at" => "2020-12-15 13:10:43"
    "updated_at" => "2020-12-15 13:10:43"
  ]
  #original: array:6 [▶]
  #changes: []
  #casts: []
  #classCastCache: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #fillable: []
  #guarded: array:1 [▶]
}

So the Quantity can be seen as 1 in the debug but when being displayed, it shows an error. Is there anything I can do to resolve this? I already tried different syntax on the controller like using "where" instead of "find" but it still does not work

Friel
  • 51
  • 4
  • 2
    In your blade file you're looping over `$cart` like it's a collection, when it's actually just 1 Cart record. Using `find()` will return 1 record, so you can't loop over it. – David Grzyb Dec 15 '20 at 14:05
  • 2
    Why are you using a `@foreach` to loop through a single model? Removing the `@foreach` and using `$cart->Quantity` should work. – Remul Dec 15 '20 at 14:06

0 Answers0