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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43111519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:46:24  来源:igfitidea点击:

Laravel: Undefined variable in blade view

phphtmllaravelviewlaravel-blade

提问by Sahand

This is AdminController.php:

这是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]);
    }
}

And this is admin.blade.php:

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

Why do I get the following error?

为什么会出现以下错误?

ErrorException in 408148d5409ae6eebf768dc5721bd0d1d9af48af.php line 9: Undefined variable: images (View: /Users/sahandz/Documents/School/Singapore/CS3226/backend/resources/views/admin.blade.php)

ErrorException in 408148d5409ae6eebf768dc5721bd0d1d9af48af.php 第 9 行:未定义变量:图像(视图:/Users/sahandz/Documents/School/Singapore/CS3226/backend/resources/views/admin.blade.php)

$imagesis clearly defined in my controller.

$images在我的控制器中明确定义。

回答by Joe

You have used $imagesas your variable name when passing the data to the view. This results in blade creating a variable called $$images.

$images将数据传递给视图时,您已将其用作变量名。这导致刀片创建一个名为 的变量$$images

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

will result in the view creating a variable called $images

将导致视图创建一个名为的变量 $images

回答by Rehan

Try passing data like this.

尝试像这样传递数据。

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

Basically,you need not use $in the variable name.

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

回答by Abdullah Al Shakib

Your code should be like this

你的代码应该是这样的

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

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

}

}

Why you use [ '$images' => $images]. that is the problem.

为什么你使用[ '$images' => $images]. 那就是问题所在。

回答by ram pratap singh

You may use this method also to pass your data to view

您也可以使用此方法将数据传递给查看

 return view('admin',compact('images'));