我一直在努力寻找这个错误的解决方案。尝试了不同的解决方案但得到了相同的结果。问题是第二个论点,我不知道它为什么不起作用?
索引页:
@extends('layouts.master')
@section('title')
Laravel Shopping Cart
@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="thumbnail">
<img src="{{$product->imgPath}}" class="img-responsive">
<div class="caption">
<h3>{{$product->title}}</h3>
<p class="description">{{$product->description}} </p>
<div class="clearfix">
<div class="price pull-left">${{$product->price}}</div>
<a href="{{route('product.addToCart', ['id'=>$product->id])}}" class="btn btn-success pull-right" role="button">Add to Cart</a>
</div>
</div>
</div>
</div>
@endforeach
</div>
@endforeach
@endsection
ProductController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Product;
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') ? Session::get('cart') : null;
$cart = new Cart($oldCart);
$cart->add($product, $product->id);
$request->session()->put('cart', $cart);
dd($request->session()->get('cart'));
return redirect()->route('product.index');
}
}
Route.php
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/', ['uses' => 'ProductController@getIndex',
'as' => 'product.index']);
Route::get('add-to-cart', [
'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']);
});
});
错误日志:
ErrorException in ProductController.php line 16:
Missing argument 2 for App\Http\Controllers\ProductController::getAddToCart()
in ProductController.php line 16
at HandleExceptions->handleError('2', 'Missing argument 2 for App\Http\Controllers\ProductController::getAddToCart()', '/home/ubuntu/workspace/app/Http/Controllers/ProductController.php', '16', array('request' => object(Request))) in ProductController.php line 16
at ProductController->getAddToCart(object(Request))
at call_user_func_array(array(object(ProductController), 'getAddToCart'), array(object(Request))) in Controller.php line 80
at Controller->callAction('getAddToCart', array(object(Request))) in ControllerDispatcher.php line 146
at ControllerDispatcher->call(object(ProductController), object(Route), 'getAddToCart') in ControllerDispatcher.php line 94
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96
at ControllerDispatcher->callWithinStack(object(ProductController), object(Route), object(Request), 'getAddToCart') in ControllerDispatcher.php line 54
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\ProductController', 'getAddToCart') in Route.php line 174
at Route->runController(object(Request)) in Route.php line 140
at Route->run(object(Request)) in Router.php line 724
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 64
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 726
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 699
at Router->dispatchToRoute(object(Request)) in Router.php line 675
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 54
答案 0 :(得分:1)
尝试将id参数添加到您定义的路线
Route::get('add-to-cart/{id}', [
'uses' => 'ProductController@getAddToCart',
'as' => 'product.addToCart'
]);
答案 1 :(得分:1)
您不会将参数ID发送到控制器和操作getAddToCart
。
你应该使用这段代码。
add-to-cart/{id}
和网址add-to-cart/5
如果id
是可选的
使用add-to-cart/{id?}
和网址add-to-cart