ErrorException 未定义的变量:

ErrorException Undefined variable:

为什么我会收到这个错误? 错误异常 未定义变量:特征(视图:C:\xampp\htdocs.....views\layouts\index.blade.php)

FeaturedController.php

 public function index()
             {
                $features = Feature::get();
                return view ('layouts.index')->with(compact('features'));
        
            }

ProductsController.php

public function index()
    {
        $products = Product::get();
        return view ('products')->with(compact('products'));
      
    }

layouts page- index.blade.php

 @yield('content')
     @foreach($features as $f)
         <li>
              <div class="prodcut-price mt-auto">
                  <div class="font-size-15">LKR {{ $f ['features_id'] }}.00</div>
             </div>
         </li>
        @endforeach

查看页面 - 索引。blade.php

@extends('layouts.index')
@section('content')

  @foreach($products as $p)
                       <div class="mb-2"><a href="../shop/product-categories-7-column-full-width.html" class="font-size-12 atext">{{ $p ['prod_sub_category'] }}</a></div>
                                            <h5 class="mb-1 product-item__title"><a href="../shop/single-product-fullwidth.html" class="text-blue font-weight-bold">{{ $p ['prod_name'] }}</a></h5>
                                            <div class="mb-2">
                                                <a href="../shop/single-product-fullwidth.html" class="d-block text-center"><img class="img-fluid" src="{{asset('/storage/admin/'.$p ['prod_image_path'] ) }}"  alt="Image Description"></a>
                                            </div>
                                            <div class="flex-center-between mb-1">
                                                <div class="prodcut-price">
                                                    <div class="atext">LKR {{ $p ['prod_price'] }}.00</div>
                                                </div>
                                                <div class="d-none d-xl-block prodcut-add-cart">
                                                    <a href="../shop/single-product-fullwidth.html" class="btn-add-cart btn-primary transition-3d-hover"><i class="ec ec-shopping-bag"></i></a>
                                                </div>

web.php

Route::resource('/products', 'ProductsController');

Route::resource('/layouts/index', 'FeaturedController@index');  

您可以将控制器更改为:

 public function index()
 {
     $features = Feature::all();
     return view ('layouts.index', compact('features'));
        
  }

A 你的 blade 你实际上应该做 @section:

@section('content')
    @foreach($features as $f)
         <li>
              <div class="prodcut-price mt-auto">
                  <div class="font-size-15">LKR {{ $f->features_id }}.00</div>
             </div>
         </li>
     @endforeach
@endsection

除了没有将您的变量适当地传递给您的 blade 视图之外,其他答案已经指出,您试图从没有设置功能的控制器访问 features

下面的控制器设置功能,然后在 layouts.index blade 文件中使用它。

FeaturedController.php

public function index()
{
    $features = Feature::get();

    return view ('layouts.index')->with(['features' => $features]);

    // or

    // return view ('layouts.index', compact('features'));
        
}

虽然此控制器设置了 products,但随后使用 blade 文件扩展了另一个包含 features 变量的 blade 文件。这就是您收到错误

的原因

ProductsController.php

public function index()
{
    $products = Product::get();

    return view ('products', compact('products'));
}

要修复它,您必须像这样在 products 旁边传递 features 变量:

ProductsController.php

public function index()
{
    $products = Product::get();
    $features = Feature::get();

    return view ('products')->with(['features' => $features, 'products' => $products]);
}

但是如果有多个 blade 文件要扩展这个 layouts.index 文件,那么这种方法是不可取的,这种情况就是 Taylor Otwell 引入 Blade Components 的原因。您现在可以将 features blade 视图和逻辑移动到一个组件,该组件可以环绕您想要或包含的任何其他文件。

文档很简单,但如果你想让我告诉你如何实施它来解决你的困境,请在下面的评论中打我。

当你使用 layout 中的数据时,你应该使用 laravel view composer 将数据共享到布局文件 ref link https://laravel.com/docs/7.x/views#view-composers

在你的AppServiceProvider.php

boot()里面添加这一行

 public function boot()
    {
       \View::composer('layouts.index', function ($view) { // here layout path u need to add
        $features = Feature::get();
         $view->with([
            'features'=>$features,
         ]);
});
}

It share data based on specif view file like here layouts.index data is send to this view so if u not send data from controller it will get data from view composer