There is a model Property.php:

class Property extends Model { protected $fillable = ['imagePath', 'title']; } 

Created 2 controllers:

 class PropertyController extends Controller { public function getIndex() { $properties = Property::all(); return view('sh.index', ['properties' => $properties]); } } class SearchController extends Controller { public function getSearch() { $properties = Property::all(); return view('sh.search', ['properties' => $properties]); } } 

routes file:

 Route::get('/', [ 'uses' => 'PropertyController@getIndex', 'as' => 'property.index' ]); Route::get('/search', [ 'uses' => 'SearchController@getSearch', 'as' => 'sh.search' ]); 

Created two identical views of search.blade.php and index.blade.php:

 @extends('layouts.master') @section('content') @foreach($properties->chunk(4) as $propertyChunk) <div class="row"> @foreach($propertyChunk as $property) <div class="col-sm-6 col-md-3"> <img src="{{ $property->imagePath }}" alt="..." class="img-responsive"> <h3>{{ $property->title }}</h3> </div> @endforeach </div> @endforeach @endsection 

The index.blade.php view works fine, search.blade.php gives the following error:

 Undefined variable: products (View: /var/www/projects/auth.laravel.com/resources/views/sh/search.blade.php) 

Why can't he define a variable?

PS: search.blade.php working page

layouts / master.blade.php:

 <body> @include('part.header') <div class="container content-body"> @yield('content') </div> @include('part.footer') </body> 
  • Check layouts.master for the presence of $products and the correct transfer of this variable - Dmitriy Simushev
  • added layouts.master ... there is no $ products variable and there is no mention !!! - Alexander
  • Is the template just without @extends ('layouts.master')? - Kostiantyn Okhotnyk
  • does not work ... gives the same error .... - Alexander
  • Well, look for where you have $products and why it is not defined. - Dmitriy Simushev

1 answer 1

In the routs, the address did not work after "/". It turned out, it was in the settings of Apache! You need to enable rewrite through the console:

 apache2ctl -M | grep -i rew sudo a2enmod rewrite sudo /etc/init.d/apache2 restart