【问题标题】:Laravel: Undefined variableLaravel:未定义的变量
【发布时间】:2015-05-15 08:24:20
【问题描述】:

我的代码有问题。

我想显示登录但我有一个学生的记录

我的 view.blade 上的未定义变量

这是我的模型

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

这是我的控制器

public function viewstudentAttendance()
{
$students = Auth::user()->id;

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

最后是我的观点。刀片

@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

我认为问题在于我的观点或我如何声明变量?请帮忙? :(

【问题讨论】:

  • @if ($students->count()) ??
  • 您将数据发送为attendances 而不是students
  • @foreach($students 为 $student)

标签: laravel laravel-4 eloquent declaration laravel-5


【解决方案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
}

在刀片模板中,使用:

@if ($studentCount > 10)

而不是

@if ($students->count())

您的学生正在返回ID,您怎么能“计数”

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

在你的刀片中你一直在做 if($students) bla bla,只是为了让你知道它的出勤率

->with('出席人数', $students);

出勤率是您的刀片将看到的变量,$student 是您推送到刀片出勤率的数据

【讨论】:

  • 谢谢!很抱歉,呵呵
  • 没问题!以前是你的情况!在这里问愚蠢的问题被打了 LOL!
猜你喜欢
  • 2019-04-29
  • 1970-01-01
  • 2019-03-01
  • 2023-03-05
  • 2018-01-26
  • 2020-09-24
  • 2021-10-31
  • 2014-06-03
  • 1970-01-01
相关资源
最近更新 更多