PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, March 16, 2022

[FIXED] Undefined foreach variable laravel

 March 16, 2022     laravel, php     No comments   

Issue

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

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'));


Answered By - Yves Kipondo
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing