Laravel: Undefined variable

家住魔仙堡 提交于 2019-12-12 01:53:34

问题


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? :(


回答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



来源:https://stackoverflow.com/questions/29022324/laravel-undefined-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!