这是AdminController.php
:
<?php
namespace App\Http\Controllers;
use Response;
use Illuminate\Support\Facades\DB;
use App\Caption;
use App\Image;
use App\Http\Controllers\Controller;
class AdminController extends Controller
{
public function admin() {
$images = Image::paginate();
return view('admin',[ '$images' => $images]);
}
}
这是admin.blade.php
:
@extends('template')
@section('title')
Admin Page
@endsection
@section('header')
@endsection
@section('main')
@if (Auth::check())
@foreach ($images as $image)
<p value='{{$image->id}}'>{{$image->content}}</p>
<form action="image/{{$image->id}}/delete" method="post">
<button type="submit">Delete caption</button>
</form>
<form action="image/{{$image->id}}/approve" method="post">
<button type="submit">Accept image</button>
</form>
@endforeach
@else
<p>Login first</p>
@endif
@endsection
@section('footer')
@endsection
为什么会出现以下错误?
ErrorException in 408148d5409ae6eebf768dc5721bd0d1d9af48af.php line 9: Undefined variable: images (View: /Users/sahandz/Documents/School/Singapore/CS3226/backend/resources/views/admin.blade.php)
$images
在我的 Controller 中明确定义。
最佳答案
在将数据传递给 View 时,您使用了 $images
作为变量名。这导致 Blade 创建一个名为 $$images
的变量。
return view('admin',[ 'images' => $images]);
将导致 View 创建一个名为 $images
关于php - Laravel: Blade View 中的 undefined variable ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43111519/