skip to Main Content
Route::get('/pizza', function () {
    $edibles = [
        'fruits' => 'Apple',
        'beverage' => 'Milo',
        'soup' => 'Egusi',
        'drink' => 'cocacola',
    ];

    return view('pizza', $edibles);
});
@foreach ($edibles as $data)
    {{ $data }} 
@endforeach

It has been saying

Undefined
ErrorException
PHP 8.1.6
9.42.2
Undefined variable $edibles

4

Answers


  1. Change this

    return view('pizza', $edibles);
    

    To this

    return view('/pizza', compact('edibles'));
    
    Login or Signup to reply.
  2. Route::get('/pizza', function(){
             $edibles = [
                'fruits' => 'Apple',
                'beverage' => 'Milo',
                'soup' => 'Egusi',
                'drink' => 'cocacola',
            ];
            return view('pizza', compact('edibles'));
        });
    
    Login or Signup to reply.
  3. You can use compact to send the data. Try this for your case:

    return view('pizza', compact('edibles'));
    
    Login or Signup to reply.
  4. There is two way to send backend data to front end.

    1. Without compact function you can send variable and its data as single variable name
      Here you can set a key name of passing data like ediblesData and get on front side with same name inside foreach loop.

    Ex: return view('pizza', ['edibles' => 'edibles']);

    1. With compact function you can send variable and its data as single variable name
      Here you cannot set a key name of passing data and you need to get same key name on front end side.

    Ex: return view('pizza', compact('edibles'));

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search