如何使用 laravel 中的控制器将多个变量传递到视图中

how to pass multiple variables in to a view using controller in laravel

这是我的控制器

public function show($id)
{
    $products = Item::find($id)->products;

    $categories = Item::all();

    return view('category.index', compact(['categories', 'products']));
}

这是我的看法

@extends('layout.front')
@section('page')
<div class="row" id="portfolio">

<!-- Page Content -->
<div class="container">

    <div class="row">

        <div class="col-lg-3" style="margin-top:20px;">

            <nav class="main-nav">
                <ul class="main-nav-ul">
                    <li style="background-color: #343A40; color: #ffffff; font-weight: 600;" id="sidebar-header"><a>Product List</a></li>
                    <li><a href="#" style="font-weight: 600;">HouseHold<span></span></a>
                        <ul>
                            @if(!empty($categories))
                                @forelse($categories as $category)
                                <li><a href="{{ route('category.show', $category->id)}}">{{ $category->name }}</a></li>

                                @empty
                                    <li>No data found</li>
                                @endforelse
                            @endif
                        </ul>
                    </li>
                    <li><a href="#" style="font-weight: 600;">Vegetables</a></li>
                    <li><a href="#" style="font-weight: 600;">Fruits</a></li>
                </ul>
            </nav>

        </div>
        <!-- /.col-lg-3 -->

        <div class="col-lg-9">


            <div class="row" style="margin-top:20px;">



                    @foreach($products as $key=>$product)
                        <div class="col-md-4 col-sm-6 portfolio-item" id="items">
                            <div class="card h-100">

                                <a href="#"><img class="card-img-top" src="/storage/{{ $product->image }}" alt="Product Image"></a>
                                <div class="card-body">
                                    <h4 class="card-title">
                                        <a href="#"><br />{{ $product->item_name}}</a>
                                    </h4>
                                    <p class="card-text" style="color: #A9A9A9;text-decoration: line-through;">LKR {{ $product->old_price}}</p>
                                    <h4 style="text-align: center; color: #fff;"><a class="waves-effect waves-light btn btn-dark btn-block">LKR {{ $product->new_price}}</a></h4>

                                </div>
                            </div>
                        </div>
                    @endforeach


            </div>
            <!-- /.row -->

        </div>
        <!-- /.col-lg-9 -->

    </div>
    <!-- /.row -->

</div>
<!-- /.container -->
</div>
@endsection

我想在同一视图中传递我的类别列表和产品项目。但是我不能将这两个变量从控制器传递到我的视图。它显示此错误。

Undefined variable: products (View: /var/www/html/hzone_new/resources/views/category/index.blade.php)

像这样更新您的控制器功能:

$categories = Item::all();
$products = Item::find($id)->products;
return view('category.index', compact('categories', 'products'));

注意:您不能在函数中定义产品变量,请定义

@if(!empty($products))
  @foreach($products as $key=>$product)
                        <div class="col-md-4 col-sm-6 portfolio-item" id="items">
                            <div class="card h-100">

                                <a href="#"><img class="card-img-top" src="/storage/{{ $product->image }}" alt="Product Image"></a>
                                <div class="card-body">
                                    <h4 class="card-title">
                                        <a href="#"><br />{{ $product->item_name}}</a>
                                    </h4>
                                    <p class="card-text" style="color: #A9A9A9;text-decoration: line-through;">LKR {{ $product->old_price}}</p>
                                    <h4 style="text-align: center; color: #fff;"><a class="waves-effect waves-light btn btn-dark btn-block">LKR {{ $product->new_price}}</a></h4>

                                </div>
                            </div>
                        </div>
                    @endforeach
@endif