Laravel: Undefined variable

im having a problem with my code.

I want to show the record of the student who login but im having an

undefined variable on my view.blade

Here’s my Model

class Attendance extends Eloquent {
  public function users()
  {
    return $this->belongsTo('User', 'id');
  }
}

Here’s my Controller
public function viewstudentAttendance()
{
$students = Auth::user()->id;

    //load view and pass users  
    return View::make('student.view')
        ->with('attendances', $students);   
}

Finally here’s my view.blade
@extends('layouts.master')

@section('head')
@parent
<title>View Attendance</title>
@stop

        {{ HTML::style('css/bootstrap.css'); }}

@section('content')



</ul>
    @if ($students->count())

<table class="table table-striped table-bordered">
    <thead>
        <tr>
    <th>ID</th>
    <th>First name</th>
    <th>Last name</th>




        </tr>
    </thead>

    <tbody>
        @foreach ($students as $students)
            <tr>
       <td>{{ $students->id }}</td> 
      <td>{{ $students->firstname }}</td>
      <td>{{ $students->lastname }}</td>    




          @endforeach

            </tr>
      {{ HTML::script('js/bootstrap.js'); }}

    </tbody>

</table>

@else
There are no attendance recorded yet
@endif
@stop

I think the problem is with my view or how i declare the variable? Please help? 🙁

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

public function viewstudentAttendance() {
    //This code turns ID ONLY Check the website out for a code that retrieves data so you can loop it.
    $students = Auth::user() - > id;
    //Code that counts number of student
    $studentCount = XXX(Google It)
    //load view and pass users  
    return View::make('student.view') - > with('attendances', $students);
    //Return StudentCount too
}

Inside your blade template, use :
@if ($studentCount > 10)

instead of
@if ($students->count())

Your students is returning ID, how could you “Count”

http://laravel.com/docs/4.2/eloquent

Inside your blade you kept doing if($students) bla bla, just to let you know it’s attendances

->with(‘attendances’, $students);

Attendances is the variable your blade will see, $student is the data you are pushing into attendances for blade


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x