rubenochoa's avatar

Shopping-cart info

Working an ecommerce site based on a project. At shopping-cart as you can see at the code bellow. It uses only item, title and price showing when someone buy something.

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;
        }
        else{
            $this->items = null;
        }
    }
    
    public function add($item, $id){
        $storedItem = ['qty' => 0, 'price' => $item->price, 'item'=> $item];
        if ($this->items){
            if (array_key_exists($id, $this->items)){
                  $storedItem = $this-> items[$id]; 
            }
        }
            $storedItem['qty']++;
            $storedItem['price'] = $item->price * $storedItem['qty'];
            
            $this->items[$id]=$storedItem;
            $this->totalQty++;
            $this->totalPrice += $item->price;
    }
   public function reduceByOne($id){
        $this->items[$id]['qty']--;
        $this->items[$id]['price'] -= $this->items[$id]['item']['price'];        
        $this->totalQty--;
        $this->totalPrice -= $this->items[$id]['item']['price'];

        if($this->items[$id]['qty']<=0){
            unset($this->items[$id]);
        }
   }
   public function removeItem($id){
    $this->totalQty -= $this->items[$id]['qty'];
    $this->totalPrice -= $this->items[$id]['item']['price'];
    unset($this->items[$id]);
   }
} 

Database:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateProductsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->string('imagePath');
            $table->string('title');
            $table->text('description');
            $table->integer('price');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('products');
    }
}

ProductTableSeeder.php

<?php

use Illuminate\Database\Seeder;

class ProductTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $product = new \App\Product([
                'imagePath' =>'storage\images\hp.jpg',
                'title' => 'Harry Potter',
                'description' =>'...',
                'price' => '10'
        ]);
        $product->save();

        $product = new \App\Product([
            'imagePath' =>'storage\images\lotr.jpg',
            'title' => 'Lord of the Rings',
            'description' =>'Great book',
            'price' => '40'
         ]);
        $product->save();

        $product = new \App\Product([
            'imagePath' =>'storage\images\ac.jpeg',
            'title' => 'Assassin`s Creed Revelations',
            'description' =>'Awesome',
            'price' => '50'
        ]);
        $product->save();

        $product = new \App\Product([
            'imagePath' =>'storage\images\laravel.jpg',
            'title' => 'Laravel for beginers',
            'description' =>'Worth the buying',
            'price' => '80'
        ]);
        $product->save();

        $product = new \App\Product([
            'imagePath' =>'storage\images\cplusplus.jpg',
            'title' => 'C++ Programming Language',
            'description' =>'For begginers',
            'price' => '120'
        ]);
        $product->save();
            }
        }

Product.php:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $filable = ['imagePath','title','description','price'];
}

blade.php

@extends('layouts.master')

@section('title')
Laravel Shopping Cart
@endsection

@section('content')
@if(Session::has('cart'))
<div class="container-fluid">
    <div class="row"> 
        <div class="col-md-6 m-auto">
        @foreach ($products as $product)
        <li class="list-group-item">
        <img src="{{ $product['imagePath'] }}">
            <span class="badge badge-secondary">Quantity: {{ $product['qty'] }}</span>
            <span class="badge badge-secondary">Item: {{ $product['item']['title'] }}</span>
            <span class="badge badge-secondary">Price: ${{ $product['price'] }}</span>                           
            <div class="btn-group">
                <button type="button" class="btn btn-primary btn-xs dropdown-toggle" data-toggle="dropdown">Action
                    <span class="caret"></span></button>
                <ul class="dropdown-menu">
                    <li><a href="{{ route('product.reduceByOne', ['id' => $product['item']['id']]) }}">Reduce by 1</a>
                    </li>
                    <li><a href="{{ route('product.remove', ['id' => $product['item']['id']]) }}">Reduce All</a></li>
                </ul>
            </div>            
        </li>
        @endforeach
    </ul>
    </div> 
  </div>
</div>
<div class="container-fluid">
     <div class="row"> 
         <div class="col-md-4 m-auto">
        <strong>Total Cost: ${{ $totalPrice }}</strong>
        </ul>
    </div>
</div>
</div>
<hr>
<div class="container-fluid">
    <div class="row"> 
        <div class="col-md-4 m-auto">
        <button><a href="{{ route('checkout') }}" type="button" class="btb-btn-success">Checkout</button></a>
    </div>
</div>
</div>
@else
<div class="container-fluid">
    <div class="row"> 
        <div class="col-md-4 m-auto">
        <h1>No item to cart</h1>
        </ul>
    </div>
</div>
</div>
@endif
@endsection

When I use <img src="{{ $product['imagePath'] }}"> says that imagePath is not difined

I hope I was cleared of what I am working. Also I hope this is the right topic to post.

Thank you.

0 likes
12 replies
Snapey's avatar

use html

Why is this posted in testing?

rubenochoa's avatar

Cause I am new here and I thought cause this issue belong to this topic

Snapey's avatar

Well whatever category (not testing) You are going to have to put more effort into your question if you want a sensible answer

rubenochoa's avatar

One thing I noticed is that does no appears the code I pasted. Also I can not change the topic anymore.

Snapey's avatar

post code with ``` on its own line before and after your code block

Snapey's avatar

When I use says that imagePath is not difined

What is the exact error?

Do you have image paths stored in the database?

also,

You have a typo here

    protected $filable = ['imagePath','title','description','price'];

should be $fillable

rubenochoa's avatar

Yes I have imagePath stored in database. The error when I use says that imagePath is not defined.

Snapey's avatar

Please format your code by putting 3 backticks ``` on a line before and after each code block

exact error please

rubenochoa's avatar
Undefined index: imagePath (View: C:\MAMP\htdocs\Laravel\shopping-cart\resources\views\shop\shopping-cart.blade.php)

You mean that way? Also I updated the post provided the exact issue.

Snapey's avatar
Snapey
Best Answer
Level 122

There is something you are not showing. How items in the cart relate to Products. You call them $products but I guess items in the cart are not products?

I notice you have Item: {{ $product['item']['title'] }}

so image should presumably be {{ $product['item']['imagePath'] }}

rubenochoa's avatar

Another little error that I could solve. That was the answer. Thank you very much. About the relate I think yes, items are not products and all the qty, price, title belongs to item.

Please or to participate in this conversation.