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

<title>View Attendance</title>

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


    @if ($students->count())

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


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


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



There are no attendance recorded yet

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


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”

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 or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x