5

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 is 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)

$imagesis clearly defined in my controller.

3
  • try this return view('admin',compact('images')); Commented Mar 30, 2017 at 7:47
  • is Image::paginate() a scope you wrote? because normally you would do something like this: $images = Image::all()->paginate(15); Commented Mar 30, 2017 at 7:49
  • the correction code should be: return view('admin',[ 'images' => $images]); Commented Mar 30, 2017 at 9:06

4 Answers 4

9

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

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

will result in the view creating a variable called $images

Sign up to request clarification or add additional context in comments.

1 Comment

great, I wasted my 4 hours and finally reached you
2

Try passing data like this.

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

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

Comments

1

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.

Comments

1

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.