Laravel: variable Undefined as blade

This is 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 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 does the following error occur?

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

      

$images

is well defined in my controller.

Thank.

+3


source to share


4 answers


You used $images

as a variable name when passing data to the view. This causes the client to create a variable $$images

.

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

      



will result in a view creating a variable named $images

+5


source


Try to pass data like this.

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

      



Basically, you don't need to use $

in the variable name.

0


source


Your code should look like this

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

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

      

}

Why are you using [ '$images' => $images]

. This is problem.

0


source


You can also use this method to pass your browsing data

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

      

0


source







All Articles