请帮我解决此错误。 我正在使用Laravel 5.4开发Shopping-Cart系统。我正在处理按钮“添加到购物车”。
**Web.app(routes)**
<?php /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! |
*/
Route::get('/', [
'uses'=>'ProductController@getIndex', 'as'=>'product.index'
]);
Route::get('/add/{id}', [
'uses' => 'ProductController@getAddToCart',
'as' => 'product.addToCart' ]);
Route::group(['prefix'=>'user'], function(){ Route::group(['middleware'=>'guest'], function() {
Route::get('/signup',[
'uses'=>'UserController@getSignup', 'as'=>'user.signup' ]);
Route::post('/signup',[
'uses'=>'UserController@postSignup', 'as'=>'user.signup'
]);
Route::get('/signin',[
'uses'=>'UserController@getSignin', 'as'=>'user.signin'
]);
Route::post('/signin',[
'uses'=>'UserController@postSignin', 'as'=>'user.signin'
]); });
Route::group(['middleware'=>'auth'], function (){ Route::get('/profile',[ 'uses'=>'UserController@getProfile', 'as'=>'user.profile'
]);
Route::get('/logout',[ 'uses'=>'UserController@getLogout', 'as'=>'user.logout'
]); });
});
Auth::routes();
Route::get('/home', 'HomeController@index');
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, $qty) {
$storedItem = ['qty'=>0, 'price'=>$this->price, 'item'=> $item]; if($this->items) { if(array_key_exists($id, $this->items)) {
$storedItem = $this->items[$id]; } }
$storedItem['qty'] += $qty; $storedItem['price'] = $item->price * $storedItem['qty']; $this->items[$id] = $storedItem; $this->totalQty += $qty; $this->totalPrice += $item->price; } }
<?php namespace App\Http\Controllers; namespace App\Http\Controllers;
use Cart; use App\Product; use Illuminate\Http\Request;
use App\Http\Requests; use Session; class ProductController extends Controller {
public function getIndex()
{
$products = Product::all();
return view('shop.index', ['products' => $products]);
}
public function getAddToCart(Request $request, $id) {
$product = Product::find($id);
$oldcart = Session::has('cart')?$request->session()->get('cart'):null;
$cart = new Cart($oldCart);
$cart->add($product, $product->id);
$request->session()->put('cart', $cart);
return redirect()->route('product.index');
}
public function getCart() {
if (!Session::has('cart')) {
return view('shop.shopping-cart');
}
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);
return view('shop.shopping-cart', ['products' => $cart->items, 'totalPrice' => $cart->totalPrice]);
} }
错误:its been two days could not find solution. Please help!
编辑:这是我的index.blade.php
@extends('layouts.master')
<link rel="stylesheet" href="{{URL::to('css/app.css')}}">
@section('title')
Find Stuff
@endsection
@section('content')
@foreach($products->chunk(3) as $productChunk)
<div class="row">
@foreach($productChunk as $product)
<div class="col-sm-6 col-md-4">
<div class="i">
<div class="thumbnail">
<img src="{{$product->imagePath}}" alt="Harry Potter Books" style="max-height:200px" class="img-responsive">
<div class="caption">
<h3><b>{{$product->title}}</b></h3>
<p>{{$product->description}}</p>
<div class="clearfix">
<div style="color:purple; font-size:16px"><b>${{$product->price}}</b></div><br>
<a href=**"{{route('product.AddToCart',[ 'id' => $product->id])}}"** class="btn btn-primary" role="button">Add To Cart</a>
<a href="#" class="btn btn-success" role="button">Buy</a>
</div>
</div>
</div>
</div>
</div>
@endforeach
</div>
@endforeach
@endsection
答案 0 :(得分:0)
在您的错误图片中,您没有提供localhost:8000/add
。由于您的路线接受 ID Route::get('/add/{id}'
因此,如果您没有提供并且ID,则会引发错误。
您需要提供add/1
或add/2
等ID。
答案 1 :(得分:0)
您的ProductController有两个名称空间声明。删除一个。
比use Cart;
等。必须在课堂内。
Cart.php文件应该有首字母大写。