简体   繁体   中英

undefined variable in laravel view

This is my controller

public function document(){
    $file = File::paginate(6);
    return view('admin.document',compact('file'));
}

and this is my view

@foreach ($file as $key => $value )
<tr class="file{{$value->id}}">
    <td>{{ $no++ }}</td>
    <td>{{$value->title}}</td>
    <td>{{$value->file}}</td>
    <td>{{$value->status}}</td> 
    <td>{{$value->created_at}}</td>
    <td>
        <a href="#" class="show-modal btn btn-info btn-sm" data-id="{{$value->id}}" data-title="{{$value->title}}" data-status="{{$value->status}}">
            <i class="fa fa-eye"></i>
        </a>
        <a href="#" class="edit-modal btn btn-warning btn-sm" data-id="{{$value->id}}" data-title="{{$value->title}}" data-status="{{$value->status}}">
            <i class="glyphicon glyphicon-pencil"></i>
        </a>
        <a href="#" class="delete-modal btn btn-danger btn-sm" data-id="{{$value->id}}" data-title="{{$value->title}}" data-status="{{$value->status}}">
            <i class="glyphicon glyphicon-trash"></i>
        </a>
    </td>  
@endforeach 

I don't know why I'm getting error

我没有看到$no变量的初始化,但是您可以在刀片服务器模板中使用它。

检查您要在$ no ++中使用的$ no变量

Like some people noticed $no variable is the guilty one.

Instead of having a counter starting from Laravel 5.3 there is a new handy $loop variable. The $loop variable is a stdClass object that provides meta information about the loop you're currently inside. It exposes the following properties:

  • index: the 0-based index of the current item in the loop; 0 would mean "first item"
  • iteration: the 1-based index of the current item in the loop; 1 would mean "first item"
  • remaining: how many items remain in the loop; if current item is first of three, would return 2
  • count: the count of items in the loop
  • first: boolean; whether this is the first item in the loop
  • last: boolean; whether this is the last item in the loop depth: integer; how many "levels" deep this loop is; returns 1 for a loop, 2 for a loop within a loop, etc.
  • parent: if this loop is within another @foreach loop, returns a reference to the $loop variable for the parent loop item; otherwise returns null

In your case you will need $loop->iteration .

<td>{{ $loop->iteration }}</td>

You got the error because: you do not define variable $no .

Define as $no = 1 before the loop...

Example:

@php($no = 1)

try this

your controller

public function document(){
   $data['file'] = File::paginate(6);
   return view('admin.document',$data);
}

foreach in your view

@foreach ($file as $value )
@endforeach

 @php $no = 0; @endphp @foreach ($file as $key=> $value) <tr class="file{{$value->id}}"> <td>{{ $no++ }}</td> <td>{{$value->title}}</td> <td>{{$value->file}}</td> <td>{{$value->status}}</td> <td>{{$value->created_at}}</td> <td> <a href="#" class="show-modal btn btn-info btn-sm" data-id="{{$value->id}}" data-title="{{$value->title}}" data-status="{{$value->status}}"> <i class="fa fa-eye"></i> </a> <a href="#" class="edit-modal btn btn-warning btn-sm" data-id="{{$value->id}}" data-title="{{$value->title}}" data-status="{{$value->status}}"> <i class="glyphicon glyphicon-pencil"></i> </a> <a href="#" class="delete-modal btn btn-danger btn-sm" data-id="{{$value->id}}" data-title="{{$value->title}}" data-status="{{$value->status}}"> <i class="glyphicon glyphicon-trash"></i> </a> </td> </tr> @endforeach 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM