传递给 Illuminate\Database\Eloquent\Model::__construct() 的参数 1 必须是数组类型,对象给定,
Argument 1 passed to Illuminate\Database\Eloquent\Model::__construct() must be of the type array, object given,
我尝试添加到购物车,但我一直收到此错误
传递给 Illuminate\Database\Eloquent\Model::__construct() 的参数 1 必须是数组类型,对象给定,在 C:\xampp\htdocs\laravelcart\app\Http\Controllers\ProductController.php
中调用
这是我的代码
cart.php 型号
<?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');
}
}
我的错误似乎来自
$cart = new Cart($oldCart);
请帮助我...
这里的问题是你的构造函数。事实上,您没有调用您定义的 "constructor" ,而是执行了父构造函数。这是因为你少了一个下划线,应该是 __construct
.
然而,这样做并不是个好主意。首先,您应该调用父构造函数,此外,这种自定义构造函数在某些情况下可能会导致问题。
添加这样的方法会好很多:
public static function restoreCart($oldCart)
{
$cart = new static();
if ($oldCart) {
$cart->items = $oldCart->items;
$cart->totalQty= $oldCart->totalQty;
$cart->totalPrice = $oldCart->totalPrice;
}
return $cart;
}
然后在您的控制器中代替:
$cart = new Cart($oldCart);
您可以使用:
$cart = Cart::restoreCart($oldCart);
我尝试添加到购物车,但我一直收到此错误 传递给 Illuminate\Database\Eloquent\Model::__construct() 的参数 1 必须是数组类型,对象给定,在 C:\xampp\htdocs\laravelcart\app\Http\Controllers\ProductController.php
中调用这是我的代码
cart.php 型号
<?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');
}
}
我的错误似乎来自 $cart = new Cart($oldCart); 请帮助我...
这里的问题是你的构造函数。事实上,您没有调用您定义的 "constructor" ,而是执行了父构造函数。这是因为你少了一个下划线,应该是 __construct
.
然而,这样做并不是个好主意。首先,您应该调用父构造函数,此外,这种自定义构造函数在某些情况下可能会导致问题。
添加这样的方法会好很多:
public static function restoreCart($oldCart)
{
$cart = new static();
if ($oldCart) {
$cart->items = $oldCart->items;
$cart->totalQty= $oldCart->totalQty;
$cart->totalPrice = $oldCart->totalPrice;
}
return $cart;
}
然后在您的控制器中代替:
$cart = new Cart($oldCart);
您可以使用:
$cart = Cart::restoreCart($oldCart);