Home > front end >  Laravel undefined variable but it is
Laravel undefined variable but it is

Time:02-16

I have a problem "Undefined variable $medzisklads", this is code of my print from databases, variable $medzisklads is undefined but I have defined in MedziskladController in index.I have 3 Models Sklad, Medzisklad and Vydajsklad. And I used this same code for Sklad with change variables, which I use for Sklad and it works. But for Medzisklad and Vydajsklad dont work and show me error with undefined variable. enter image description here

This is my foreach in index.blade.php.

 @foreach($medzisklads as $medzisklad)
   <br>
        {{$medzisklad->medzidatle}} {{$medzisklad->medzimandle}} {{$medzisklad->medzimarcipan}} {{$medzisklad->medziorechy}}
           
   @endforeach

and this is my MedziskladController

public function index()
{
    $medzisklads = Medzisklad::all();
   return view('sklads.index')-> with('medzisklads', $medzisklads);
}

and this is my route

Route::resource('medzisklad', 'App\Http\Controllers\MedziskladController');

Medzisklad is model in databases.

CodePudding user response:

pass array in with the method

public function index()
{
   $medzisklads = Medzisklad::get();
   return view('sklads.index')->with(['medzisklads' => $medzisklads]);
}

CodePudding user response:

Try this

public function index()
{
   $medzisklads = Medzisklad::get();
   return view('sklads.index', compact('medzisklads'));
}

CodePudding user response:

Try this

public function index()
   {
       $medzisklads = Medzisklad::get();
       return view('sklads.index', ['medzisklads'=>$medzisklads]);
   }

  • Related