Laravel:刀片视图中的未定义变量

时间:2017-03-30 07:43:41

标签: php html laravel view blade

这是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

感谢。

4 个答案:

答案 0 :(得分:5)

在将数据传递给视图时,您已使用angular.module('app', ['ionic', 'chatsCtrl']) .run(function($state, $ionicPlatform) { window.FirebasePlugin.onNotificationOpen(function(notification) { sampleService.setData(notification) } } .controller('chatsCtrl', function($scope,sampleService) { $scope.chats = sampleService.getData() }); .factory('sampleService', function() { var data; return { getData : function(){ return data}, setData: function(param){ data = param}, } }); 作为变量名。这会导致刀片创建一个名为$images的变量。

$$images

将导致视图创建名为return view('admin',[ 'images' => $images]);

的变量

答案 1 :(得分:0)

尝试传递这样的数据。

$images = Image::paginate();
return view("admin")->with('images',$images);

基本上,您不需要在变量名中使用$

答案 2 :(得分:0)

您的代码应该是这样的

public function admin() {
    $images = Image::paginate();

    return view('admin',[ 'images' => $images]);

}

为什么使用[ '$images' => $images]。这就是问题。

答案 3 :(得分:0)

您也可以使用此方法将数据传递给视图

 return view('admin',compact('images'));
相关问题