Shlomi Atia Shlomi Atia - 2 months ago 21
PHP Question

Passing an array from controller to view laravel using compact

I'm using laravel 5.3 (make:auth automatic register and user authentication generator), and I would like to let user choose their tags in the registration form.

I wanna pass

$tags = App\Tag::all();
to the
register.blade.php
file located in
views\auth\register.blade.php
.

I found this method:

public function showRegistrationForm()
{
return view('auth.register');
}


and I would like to do:

public function showRegistrationForm()
{
$tags = App\Tag::all();
return view('auth.register', compact($tags));
}


but I get undefined variable
'tags'
when trying to reach the
register.blade.php
file.

Answer

Don't feed the variable itself, provide the variable name when using compact.

return view('auth.register', compact('tags'));