use html
Why is this posted in testing?
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.
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'] }}
Please or to participate in this conversation.