doupang4126 2017-03-10 21:13
浏览 78
已采纳

Laravel Framework 5.4:购物车系统 - 添加到购物车按钮不起作用

Please help me with this error. I am working on Shopping-Cart system using Laravel 5.4. I was working on button "Add To 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;  } }

ProductController.php

<?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]);
    } }

Error: its been two days could not find solution. Please help!

EDIT: Here's my 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> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <a href="#" class="btn btn-success" role="button">Buy</a>
      </div>
     </div>
   </div>
  </div>
</div>

@endforeach
</div>
@endforeach
@endsection

EDIT 2: enter image description here

  • 写回答

2条回答 默认 最新

  • doufang6268 2017-03-11 15:31
    关注

    In your error image you didn't provide localhost:8000/add. Since your route accept an ID Route::get('/add/{id}' So if you didn't provide and id it will throw an error.

    You need to provide an id like add/1 or add/2 etc..

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀