Search code examples
phplaravele-commerce

ErrorException Undefined variable:


Why I am getting this error? ErrorException Undefined variable: features (View: 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

view page - 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');  

Solution

  • as u r using data in layout u should use laravel view composer to share data to layout file ref link https://laravel.com/docs/7.x/views#view-composers

    in your AppServiceProvider.php

    inside boot() add this line

     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