0

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

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.