【问题标题】:ErrorException Undefined variable:ErrorException 未定义的变量:
【发布时间】:2020-09-03 00:39:44
【问题描述】:

为什么我会收到此错误? 错误异常 未定义变量:features(查看: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'));
      
    }

布局页面-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

查看页面 - index.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');  

【问题讨论】:

  • 不需要只返回视图 ('layouts.index', compact('features'));
  • @AlzafanChristian 即使我更改它也会引发相同的错误。
  • 你能用完整的html编辑吗,因为我在那里看到@yield,应该是@extends()
  • 如果我是对的,当你访问 ProductsController@index 时会出现错误,那么你应该将 Features::get() 传递给 compact('products', 'features');因为 $features 没有传递给父视图

标签: php laravel e-commerce


【解决方案1】:

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

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

FeaturedController.php

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

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

    // or

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

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

ProductsController.php

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

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

要修复它,您必须将features 变量与products 一起传递,如下所示:

ProductsController.php

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

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

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

文档很简单,但如果您想让我向您展示如何实现它来解决您的困境,请在下面的评论中联系我。

【讨论】:

  • 我建议改为 View::composer()
  • 我个人认为 laravel 7 中的新组件功能允许您将其包含在 &lt;x&gt;&lt;/x&gt; 前缀中更加灵活。但如果他不使用 laravel 7,那么我想 view composer 会做。
  • 我知道,但是他需要知道基本的,而且好像他用的是7.x,只是我的意见xD
  • 好吧,仅仅因为我们是老头并不意味着新孩子必须学习可能已经过时的技术,因为它可能仍然在 Laravel 框架中,但在低端它只是距离被标记 deprecated 还差几个版本。只是我的意见。
  • nvm,我建议现在使用 View::composer,然后如果他使用 7.x 版本,他可以在答案中使用新的组件功能,在 7.x 视图中,composer 仍然存在并且无需使用额外的刀片,更易于管理。只是建议改进您的答案。
【解决方案2】:

当你使用 layout 中的数据时,你应该使用 laravel view composer 来共享数据到布局文件引用链接 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,
         ]);
});
}

它基于特定视图文件共享数据,如layouts.index 数据发送到此视图,因此如果您不从控制器发送数据,它将从 view composer 获取数据

【讨论】:

  • 这个视图作曲家到底发生了什么?你能解释一下吗?
  • 它基于特定的视图文件共享数据,例如这里layouts.index 数据被发送到这个视图,所以如果你不从控制器发送数据,它将从view composer 获取数据
  • 我在 Laraevl 文档的帮助下自学 Laravel。您能否建议我学习 Laravel 的其他资源。 @Kamlesh 保罗
  • @Zeenath 我也自学了所有的东西,为了学习 laravel doc 和 laracasts.com 这是最好的,如果它解决了你的问题,请标记这个答案
【解决方案3】:

您可以将控制器更改为:

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

你应该用@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

【讨论】:

  • 我要了一个完整的 html,我看到了同样的 xD
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
  • 2018-01-26
  • 2020-09-24
  • 2018-11-29
  • 2020-04-16
  • 1970-01-01
相关资源
最近更新 更多