【问题标题】:Undefined variabel : shop view at foreach list item shop未定义的变量:foreach 列表项商店的商店视图
【发布时间】:2021-10-17 18:39:48
【问题描述】:

这是错误

![这是错误][1]

====

我很困惑让页面直接转到购物清单。此处的错误表示未在循环中定义 shop 变量以显示已添加到购物车的商品。但是数据已经录入数据库了

cart.blade.php

<div class="site-section">

    <div class="container">
        <div class="row mb-5">
            <form class="col-md-12" method="post">
                <div class="site-blocks-table">
                    <table class="table table-bordered">

                        <thead>
                             <tr>
                                <th class="product-thumbnail">Image</th>
                                <th class="product-name">Product</th>
                                <th class="product-price">Price</th>
                                <th class="product-quantity">Quantity</th>
                                <th class="product-total">Total</th>
                              </tr>
                        </thead>
                        <tbody>
                        @foreach( $shop as $customer )
                        <tr>
                            <th class ="row">{{ $loop->iteration }}</th>
                            <td>{{ $customer->image }}</td>
                            <td>{{ $customer->product }}</td>
                            <td>{{ $customer->price }}</td>
                            <td>{{ $customer->quantity }}</td>
                            <td>{{ $customer->total_price_prdt }}</td>
                            <td>
                            <a href="#" class="btn btn-primary btn-sm">X</a>
                            </td>
                        </tr>
                        @endforeach
                        </tbody>
                    </table>
                </div>
            </form>
        </div>

        <div class="row">
            <div class="col-md-6">
                <div class="row mb-5">
                    <div class="col-md-6 mb-3 mb-md-0">
                        <button class="btn btn-primary btn-sm btn-block">Update Cart</button>
                    </div>
                    <div class="col-md-6">
                        <button class="btn btn-outline-primary btn-sm btn-block">Continue Shopping</button>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-12">
                        <label class="text-black h4" for="coupon">Coupon</label>
                        <p>Enter your coupon code if you have one.</p>
                    </div>
                    <div class="col-md-8 mb-3 mb-md-0">
                        <input type="text" class="form-control py-3" id="coupon" placeholder="Coupon Code">
                    </div>
                    <div class="col-md-4">
                        <button class="btn btn-primary btn-sm">Apply Coupon</button>
                    </div>
                </div>
            </div>
            <div class="col-md-6 pl-5">
                <div class="row justify-content-end">
                    <div class="col-md-7">
                        <div class="row">
                            <div class="col-md-12 text-right border-bottom mb-5">
                                <h3 class="text-black h4 text-uppercase">Cart Totals</h3>
                            </div>
                        </div>
                        <div class="row mb-3">
                            <div class="col-md-6">
                                <span class="text-black">Subtotal</span>
                            </div>
                            <div class="col-md-6 text-right">
                                <strong class="text-black"></strong>
                            </div>
                        </div>
                        <div class="row mb-5">
                            <div class="col-md-6">
                                <span class="text-black">Total</span>
                            </div>
                            <div class="col-md-6 text-right">
                                <strong class="text-black"></strong>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-12">
                                <button class="btn btn-primary btn-lg py-3 btn-block"
                                    onclick="window.location='checkout.html'">Proceed To Checkout</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

购物车控制器

<?php

namespace App\Http\Controllers;

use App\addcart;
use Illuminate\Support\Facades\DB;
use App\cart;
use Illuminate\Http\Request;
use Auth;
use App\User;

class cartController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // return view('customers/singleshop');
    }

    public function addToCart(Request $request)
    {

        // $product = DB::select('select * from add_cart where id='.$id);

        // $user = Auth::user();
        // $ac = new addcart();
        // $ac->product = $request->product;
        // $ac->product_price = $request->price;
        // $ac->item_model = $request->model;
        // $ac->quantity = $request->qty;

        // $ac->save();
        // // $shop_id = $ac->id;
        // return redirect('/cart');

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        $user = Auth::user();
        $ac = new addcart();
        $ac->product = 'tanktop';
        // $ac->product = !empty($request->product) ? $request->product : 'default value';
        $ac->product_price = 500.000;
        $ac->item_model = $request->model;
        $ac->quantity = $request->qty;

        $ac->save();
        // $shop_id = $ac->id;
        return redirect('cart', $shop);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\cart  $cart
     * @return \Illuminate\Http\Response
     */
    public function show(cart $cart)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\cart  $cart
     * @return \Illuminate\Http\Response
     */
    public function edit(cart $cart)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\cart  $cart
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, cart $cart)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\cart  $cart
     * @return \Illuminate\Http\Response
     */
    public function destroy(cart $cart)
    {
        //
    }
}

  [1]: https://i.stack.imgur.com/3ukSj.png

【问题讨论】:

  • 未定义变量商店。没有变量 $shop
  • 将“数据输入数据库”不会使其在视图中可用。 Passing Data To Views
  • 请显示您的购物车控制器索引函数,因为这是缺少 $shop 变量的路线。
  • 是的,我已经添加了所有代码控制器购物车@JordanCasey

标签: php laravel-5 laravel-blade


【解决方案1】:

您的购物车控制器将需要渲染视图或重定向到使用 shop 变量渲染视图的路由。我希望这是您的索引。由于您的索引未定义,您可以将 shop 变量添加到视图中并将其绑定如下。我相信你的商店是 $ac 变量。另外,我假设您的刀片视图是 /resources/views/customers.cart.blade.php 根据您的后续评论。如果 $ac 不是您的产品列表,请将 $ac 替换为预期的列表。

public function store(Request $request)
    {

        $user = Auth::user();
        $ac = new addcart();
        $ac->product = 'tanktop';
        // $ac->product = !empty($request->product) ? $request->product : 'default value';
        $ac->product_price = 500.000;
        $ac->item_model = $request->model;
        $ac->quantity = $request->qty;

        $ac->save();
        // $shop_id = $ac->id;
        return view('customers/cart',['shop'=>$ac]);
    }

【讨论】:

  • 我的文件夹视图 /resources/views/customers/cart.blade.php @JordanCasey
  • @BayuRamadhan 我已经相应地更新了代码
【解决方案2】:

当您想要重定向到其他页面并想要使用新变量时,您最常使用compact, with 方法使该变量可用。

我的建议是:


In your Controller/@store

return redirect()->route('Merchant view')->with( ['shop => $shop] );

会有用的

【讨论】:

    猜你喜欢
    • 2011-09-13
    • 2016-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多