Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Undefined foreach variable laravel

hello guys im having a problem with passing variable from my controller to views, as it does not identify its variable, here is my code:

RegisterController.php

use App\angkatan;
public function index()
    {            
        $select = Angkatan::all();
        return view('/auth/register')->with('name', $select);
    }

My Route

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

web.php

Route::get('register', 'RegisterController@index');

and my view

register

@foreach($name as $ps)
@endforeach

the error say

Undefined variable: name (0)

im very thankful if anyone can help

>Solution :

You are just passing the wrong way $select variable to your view.

when you use Illuminate\View\View::with method you should pass an associative array which as key => value pairs

  return view('/auth/register')->with(['name' => $select]);

You can also use compact which allows to pass variable which are accessible inside of the scope of your controller to the view and the string passed as argument to that function will be the name of variable accessible inside of the view file

$select = Angkatan::all();
return view('/auth/register', compact('select'));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading