Laravel error: Trying to get property 'id' of non-object
Laravel error: Trying to get property 'id' of non-object
我必须从购物车中删除产品,但我遇到以下错误:
Trying to get property 'id' of non-object (View:
/home/acer/test/project_basket/basket/resources/views/cart.blade.php).
我也尝试过不同的方式,...删除会话,但我不知道这似乎有点困难,因为我只需要删除一个产品。
对于购物车中每个相同的添加产品,我都会创建每次递增 1 的徽章。
cart.blade.php
@foreach($products as $product)
<tr>
<td><img src="https://via.placeholder.com/400x200/" class="img-cart"></td>
<td><strong>{{ $product['item']['title'] }}</strong></td>
<td>
<form class="form-inline">
<input class="form-control" type="text" value="{{ $product['qty'] }}" style="margin-right: 20px" >
<!--<button rel="tooltip" class="btn btn-default"><i class="fa fa-pencil"></i></button>-->
<form method="POST" action="{{ route('remove.item', $product->id) }}" >
@csrf
{{ method_field('DELETE') }}
<input type="submit" value="Delete" onclick="return confirm('Are you sure?')" class="btn btn-primary" />
</form>
</form>
</td>
<td>${{ $product['price'] }}</td>
</tr>
@endforeach
ProductController.php
<?php
namespace App\Http\Controllers;
use App\Cart;
use App\Product;
use Illuminate\Http\Request;
use Session;
class ProductController extends Controller
{
/**
*@return \Illuminate\Http\Response
*/
public function index()
{
$products = Product::all();
return view('home', ['products'=> $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('home');
}
public function getCart()
{
if (!Session::has('cart')) {
return view('cart');
}
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);
return view('cart', ['products' => $cart->items, 'totalPrice' => $cart->totalPrice]);
}
/**
* Remove the specified resource from storage.
*
* @param \App\Product $product
* @return \Illuminate\Http\Response
*/
public function destroy(Product $product)
{
$product->delete();
return redirect()->route('cart');
}
}
路线:
Route::get('/add-cart/{id}', [
'uses' => 'ProductController@getAddToCart',
'as' => 'addToCart'
]);
Route::get('/cart', [
'uses' => 'ProductController@getCart',
'as' => 'shoppingCart'
]);
Route::get('/destroy/{id}', [
'uses' => 'ProductController@destroy',
'as' => 'remove.item']);
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;
}
}
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;
}
}
像下面这样修改你的代码
cart.blade.php
<form method="POST" action="{{ route('remove.item', $product) }}" >
@csrf
{{ method_field('DELETE') }}
<input type="submit" value="Delete" onclick="return confirm('Are you sure?')" class="btn btn-primary" />
</form>
路线
Route::get('/destroy/{product}', [
'uses' => 'ProductController@destroy',
'as' => 'remove.item']);
那么它应该可以解决你的问题
尝试用 $product['id']
替换 $product->id
。所以这一行:
<form method="POST" action="{{ route('remove.item', $product->id) }}" >
对此的更改:
<form method="POST" action="{{ route('remove.item', $product['id']) }}" >
你必须这样使用
<form method="POST" action="{{route('remove.item',['product'=>$product['id']])}}" >
或
<form method="POST" action="{{route('remove.item',['product'=>$product->id])}}" >
我必须从购物车中删除产品,但我遇到以下错误:
Trying to get property 'id' of non-object (View: /home/acer/test/project_basket/basket/resources/views/cart.blade.php).
我也尝试过不同的方式,...删除会话,但我不知道这似乎有点困难,因为我只需要删除一个产品。 对于购物车中每个相同的添加产品,我都会创建每次递增 1 的徽章。
cart.blade.php
@foreach($products as $product)
<tr>
<td><img src="https://via.placeholder.com/400x200/" class="img-cart"></td>
<td><strong>{{ $product['item']['title'] }}</strong></td>
<td>
<form class="form-inline">
<input class="form-control" type="text" value="{{ $product['qty'] }}" style="margin-right: 20px" >
<!--<button rel="tooltip" class="btn btn-default"><i class="fa fa-pencil"></i></button>-->
<form method="POST" action="{{ route('remove.item', $product->id) }}" >
@csrf
{{ method_field('DELETE') }}
<input type="submit" value="Delete" onclick="return confirm('Are you sure?')" class="btn btn-primary" />
</form>
</form>
</td>
<td>${{ $product['price'] }}</td>
</tr>
@endforeach
ProductController.php
<?php
namespace App\Http\Controllers;
use App\Cart;
use App\Product;
use Illuminate\Http\Request;
use Session;
class ProductController extends Controller
{
/**
*@return \Illuminate\Http\Response
*/
public function index()
{
$products = Product::all();
return view('home', ['products'=> $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('home');
}
public function getCart()
{
if (!Session::has('cart')) {
return view('cart');
}
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);
return view('cart', ['products' => $cart->items, 'totalPrice' => $cart->totalPrice]);
}
/**
* Remove the specified resource from storage.
*
* @param \App\Product $product
* @return \Illuminate\Http\Response
*/
public function destroy(Product $product)
{
$product->delete();
return redirect()->route('cart');
}
}
路线:
Route::get('/add-cart/{id}', [
'uses' => 'ProductController@getAddToCart',
'as' => 'addToCart'
]);
Route::get('/cart', [
'uses' => 'ProductController@getCart',
'as' => 'shoppingCart'
]);
Route::get('/destroy/{id}', [
'uses' => 'ProductController@destroy',
'as' => 'remove.item']);
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;
}
}
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;
}
}
像下面这样修改你的代码
cart.blade.php
<form method="POST" action="{{ route('remove.item', $product) }}" >
@csrf
{{ method_field('DELETE') }}
<input type="submit" value="Delete" onclick="return confirm('Are you sure?')" class="btn btn-primary" />
</form>
路线
Route::get('/destroy/{product}', [
'uses' => 'ProductController@destroy',
'as' => 'remove.item']);
那么它应该可以解决你的问题
尝试用 $product['id']
替换 $product->id
。所以这一行:
<form method="POST" action="{{ route('remove.item', $product->id) }}" >
对此的更改:
<form method="POST" action="{{ route('remove.item', $product['id']) }}" >
你必须这样使用
<form method="POST" action="{{route('remove.item',['product'=>$product['id']])}}" >
或
<form method="POST" action="{{route('remove.item',['product'=>$product->id])}}" >