php - 如何在 laravel 中使用 Controller 将多个变量传递给 View

标签 php mysql laravel laravel-blade

这是我的 Controller

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

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

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

最佳答案

像这样更新你的 Controller 函数:

$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

关于php - 如何在 laravel 中使用 Controller 将多个变量传递给 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47237833/

相关文章:

php - 使用 MySQL 从表中获取特定列表,然后获取剩余列表

c# - 读取数据库出错,方法或操作未实现

php - Laravel 中的依赖注入(inject)

php - laravel 使用其关系 ID 获取 Eloquent 数据

PHP fatal error : Call to undefined function utf8_decode()

php - 从逗号分隔的数字php获取int值

php - 使用 php、ffmpeg 和 proc_open 逐帧读取视频

c# - Nunit MySQL 错误

php - 如何让我的网站在用户关闭浏览器时自动注销?

laravel:在 null 上调用成员函数 delete()