Kanav
Kanav

Reputation: 2819

Variable not defined in view in laravel 4

I've just started to learn laravel. These are the steps i'm following:.

  1. Created a route in routes.php
  2. Created two files in view folder and connected them using yield() and extends().
  3. Created a model in models folder

I'm trying to fetch data from a table named as registrations but it's saying variable not defined (ErrorException: Undefined variable: userdata (View: /var/www/laravel/app/views/registrations.blade.php) )

this is the description of table:

+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name  | varchar(255)     | NO   |     | NULL    |                |
| email | varchar(255)     | NO   | UNI | NULL    |                |
+-------+------------------+------+-----+---------+----------------+

This is the routes.php file

<?php

Route::get('registrations', function(){
    $userdata = registration::all();    
    return View::make('registrations')->with('registrations', $userdata);
});

this is registrations.blade.php located in views folder

@extends('layout')

@section('content')
    @foreach($userdata as $data)
        <p>{{ $data->name }}</p>
    @endforeach
@stop

this is layout.blade.php located in views folder

<!DOCTYPE HTML>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <h1>layout</h1>
        @yield('content')
    </body>
</html>

this is the registration.php file located in models folder

<?php

class registration extends Eloquent {};

?>

i'm unable to find the exact problem

Upvotes: 0

Views: 518

Answers (1)

petercoles
petercoles

Reputation: 1822

In your controller you pass the $userdata collection received from your model as 'registrations', so your view will receive this collection as $registrations.

So from what I can see in your question, the following should work:

@extends('layout')

@section('content')
    @foreach($registrations as $data)
        <p>{{ $data->name }}</p>
    @endforeach
@stop

Upvotes: 2

Related Questions