i tried adding to cart an i keep getting this error Argument 1 passed to Illuminate\Database\Eloquent\Model::__construct() must be of the type array, object given, called in C:\xampp\htdocs\laravelcart\app\Http\Controllers\ProductController.php
here is my code
cart.php model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Cart extends Model
{
//
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;
}
}
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;
}
}
product controller
<?php
namespace App\Http\Controllers;
use App\Product;
use Session;
use App\Cart;
use Illuminate\Http\Request;
class ProductController extends Controller
{
//
public function getIndex()
{
$products = Product::all();
return view('shop.index', compact('products'));
}
public function getAddToCart(Request $request, $id)
{
$product = Product::find($id);
$oldCart = Session::has('cart') ? Session::get('cart') : null;
$cart = new Cart($oldCart);
$cart->add($product, $product->id);
$request->session()->put('cart', $cart);
return redirect()->route('shop.index');
}
}
my error seems to be coming from the $cart = new Cart($oldCart); pls help me...
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire