布局中的未定义变量错误(Laravel)

时间:2019-10-16 22:31:10

标签: php laravel laravel-blade

im试图从laravel刀片的布局中产生一个部分,但问题是在我创建var时未定义var

这是我的代码:

web.php:

Route::get('/pr','ProductController@carousel');

ProductController:

    public function carousel()
    {
        $Proudcts= Product::select('name','amount','dis_amount','stock','company_id','category_id','pic_url')->get();
        return view('product-carousel')->with('products', $Proudcts);
    }

product-carousel.blade.php:

@include('include.head')
<body>
@section('product-carousel')
<div class="owl-carousel owl-theme">
@foreach($products as $product)
<div class="product-carousel item ">
    <a>
        <img  src="{{$product->pic_url}}" style='height: 400px; width: 100%; object-fit: contain'/>
        <h4 lang="en_US" class="product-name-show">{{$product->name}}</h4>
        <h3 class="amount-for-product">
            Buy
        </h3>
    </a>
</div>
@endforeach
</div>
<script src="/js/owl.js" type="text/javascript" language="JavaScript"></script>
@endsection
</body>
</html>

index.blade.php:

@include('include.head')
@extends('product-carousel')
@yield('product-carousel')

如您所见,我在“ index.blade.php”中扩展了“ product-carousel.blade.php”并添加了“ @section('product-carousel')”,问题是 $ products < / strong>在“ index.blade.php”中不存在

但是正在使用“ product-carousel.blade.php”

这是错误: 未定义的变量:产品

1 个答案:

答案 0 :(得分:2)

如果您希望在存在视图时就可以使用变量,请在AppServiceProvider的{​​{1}}函数中使用视图编辑器

boot

希望这会有所帮助