-5

My $links and $sliderimages variables work great. The $klas variable has been made by the same way like the first two I said, and for some reason it says that it is undefined variable.

First $klas was meant for another view, however I decided to test it out on my home page view where the other 2 variables were working were, but when i have put the $klas the same error occurs.

Route::get('/', function () {
        $sliderimages = App\Sliderimage::all();
        $links = App\Link::all();
        $klas = App\Kla::all();
        return view('home', compact('sliderimages'), compact('links'), compact('klas'));
    });

That is on web.php file

 @foreach($links as $link)
        <div class="col-lg-4">
                <img src="{{ Voyager::image( $link->image ) }}"  class="rounded-circle"  width="140" height="140" style="margin-left:25%">
          <h2>"{{$link->title}}"</h2>
          <p>"{{$link->text}}"</p>
          <p><a class="btn btn-secondary" href="{{$link->slug}}" role="button">За повече информация. &raquo;</a></p>
        </div>
        @endforeach
      </div>     

      @foreach($klas as $kla)
        <p>"{{$kla->klastitle}}"</p>
        @endforeach

That is on home.blade . php

The error is:

Undefined variable: klas (View: D:\xampp\htdocs\koko\diplomnata\resources\views\home.blade.php)

aynber
  • 22,380
  • 8
  • 50
  • 63
Gari
  • 35
  • 1
  • 5

1 Answers1

1

Change this:

return view('home', compact('sliderimages'), compact('links'), compact('klas'));

To this:

return view('home', compact('sliderimages', 'links', 'klas'));

Or for more longer but IMO more readable syntax:

return view('home')
    ->with('sliderimages', $sliderImages)
    ->with('links', $links)
    ->with('klas', $klas);

https://laravel.com/docs/master/views#passing-data-to-views

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
  • If you say so. The four-arguments `view()` call you show in your question is, to my knowledge, invalid and won't work. – ceejayoz Sep 18 '19 at 18:18
  • The thing is it work with sliderimages and links, but not klas. Klas for ment for another view anyways I was just trying to test it on Home view, because I thought It may work like it did with the first 2 – Gari Sep 18 '19 at 18:21
  • 1
    Have you tried both the compact() and the longer syntax? – khartnett Sep 18 '19 at 18:37
  • Yes I have. That is definetly not the problem. – Gari Sep 18 '19 at 18:53