0

I am trying to pass multiple arrays to the blade file using the following statement.

public function index()
{
    $locations = \App\Models\Location::all();
    $peopletypes = config('peopletypes');
    $eventtypes = config('eventtypes');

    return view('home')->with(compact(['locations' => $locations,
        'peopletypes' => $peopletypes,
        'eventtypes' => $eventtypes])); // tried removing compact also
}

In the HTML, I am trying to access the following way.

@foreach($eventtypes as $eventtype)
    <a class="dropdown-item ml-2" 
        id="{{ $eventtype['value'] }}" href="#">{{ $eventtype['name'] }}
    </a>
@endforeach

It is still saying that $eventtypes is undefined. Can anyone please tell me what is wrong here?

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
logeeks
  • 4,849
  • 15
  • 62
  • 93
  • Can you give the output of `dd($eventtypes);`? – STA Dec 24 '20 at 17:14
  • Does this answer your question? [How to pass data to view in Laravel?](https://stackoverflow.com/questions/18341792/how-to-pass-data-to-view-in-laravel) – miken32 Dec 24 '20 at 21:32

4 Answers4

0

Can you try this?

return view('home', compact('locations', 'peopletypes', 'eventtypes')); 
alprnkeskekoglu
  • 288
  • 1
  • 7
0

Any of these should be fine, For compact, you only need to pass the variable names and not an array.

return view('home', compact('locations', 'peopletypes', 'eventtypes')); 

return view('home', ['locations' => $locations,'peopletypes' => $peopletypes, 'eventtypes' => $eventtypes]);

return view('home')->with('locations', $locations)->with('peopletypes', $peopletypes)->with('eventtypes', $eventtypes);
Sudhanshu Kumar
  • 1,926
  • 1
  • 9
  • 21
0

For compact, Any of these below should be fine, you only need to pass the variable names. Your Function in Controller is like below:

 public function index()
    {
       $locations = \App\Models\Location::all();
       $peopletypes = config('peopletypes'); 
       $eventtypes = config('eventtypes'); 
       return view('home', compact('locations', 'peopletypes', 'eventtypes'));
Or
       return view('home', ['locations' => $locations,'peopletypes' => $peopletypes, 'eventtypes' => $eventtypes]);    
Or
       return view('home')->with('locations', $locations)->with('peopletypes', $peopletypes)->with('eventtypes', $eventtypes);
        
              
    }

and your blade.php file is:

@foreach($peopletypes as $peopletype)
     <a class="dropdown-item ml-2" id="{{$peopletype['value']}}" href="#">{{$peopletype['name']}}</a>
@endforeach

@foreach($locations as $location)
     <a class="dropdown-item ml-2" id="{{$location['value']}}" href="#">{{$location['name']}}</a>
@endforeach

@foreach($eventtypes as $eventtype)
     <a class="dropdown-item ml-2" id="{{$eventtype['value']}}" href="#">{{$eventtype['name']}}</a>
@endforeach
Haron
  • 2,371
  • 20
  • 27
0

try this in controller

    public function index()
    {
     return view('home',['locations' => \App\Models\Location::all(),
    'peopletypes' => config('peopletypes'),
    'eventtypes'=> config('eventtypes')]);
     }
Arjun bhati
  • 306
  • 1
  • 3
  • 12