Undefined variable: data in view.

Published 3 months ago by Kistlak

I'm creating a web site using a Laravel. Now I want to view all the data from the database called users. But, I got this error always - > Undefined variable: data (View: D:\wamp64\www\FinalProject\resources\views\AdminPanel.blade.php).

How can I fix this ??

Here is my Controller - AdminPanelController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;

class AdminPanelController extends Controller
{
    public function index()
    {
        return view('AdminPanel');
    }
    
    public function logout(Request $request)
    {
     $request->session()->flush();
     return redirect('/login');
    }
    
    public function AdminView()
    {
     $data=User::all();
     return view('AdminPanel')->with('data',$data);
    }    
    
}

Here is my View - AdminPanel.blade.php

<table class="table table-bordered">

<tr>
<td> Name </td>
<td> Email </td>
<td> Images </td>
</tr>


@foreach($data as $value )
<tr>
<td> {{ $value->name }} </td>
<td> {{ $value->email }} </td>
<td> <img src='{{ $value->filemove }}' style='width:100px;height:100px;'> </td>
<td> <a href="edit/{{ $value->id }}"><input type="submit" name="update" value="Update" class="btn-primary"></a> </td>
<td> <a href="delete{{ $value->id }}"><input type="submit" name="delete" value="Delete" class="btn-danger"></a> </td>
</tr>
@endforeach
</table>

Here is my routes

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('index');
});

Auth::routes();

Route::group(['middleware' => 'checkuser'],function(){
Route::get('/AdminPanel', '[email protected]')->name('AdminPanel');
});

Route::get('/AdminView', '[email protected]');

Best Answer (As Selected By Kistlak)
skliche

You would get that error when the index() method is called because you call the view without passing the data parameter and you don't seem to handle that case in the view (even though something went wrong in your post and I can't see the full view). You should not get that error when the AdminView() method is called.

tykus
tykus
3 months ago (521,110 XP)

You could share some code, or alternatively consult the relevant docs

skliche
skliche
3 months ago (147,190 XP)

You forgot to post your code ...

Basically you do something like this

Controller

public function show() {
    $users = User::all();

    return view('users.show', compact('users'));
}

View

@foreach($users as $user)
    {{ $user->name }}
@endforeach
Kistlak

@tykus , @skliche - I have updated the question. Can u tell me the error ??

skliche
skliche
3 months ago (147,190 XP)

You would get that error when the index() method is called because you call the view without passing the data parameter and you don't seem to handle that case in the view (even though something went wrong in your post and I can't see the full view). You should not get that error when the AdminView() method is called.

Snapey
Snapey
3 months ago (865,465 XP)

Amateur tip (not even pro tip), name your variables appropriately.

If it is users that you are sending to the view then send $users not $data

If you iterate over $users then one of them is a user so $user is better than $value

The code then makes more sense to the reader.

Please go back to your post and put 3 backticks ``` on a line before and after your code

Kistlak

@skliche - I am very new to laravel.. Then I have no idea.. Can demonstrate it ?? @Snapey - Done..

Cronix
Cronix
3 months ago (386,220 XP)

Do you actually get the error when you go to yoursite.com/AdminView?

Snapey
Snapey
3 months ago (865,465 XP)

Do you have a route to your index function. If so then that will error because you do not pass $data to the view.

Kistlak

@Cronix - When I goto AdminPanel page , I get this error.

@Snapey - Yes , I updated the question. How can I fix that ??

skliche
skliche
3 months ago (147,190 XP)

The easiest would be to use different views for /AdminPanel and /AdminView assuming they should show different information.

Right now you are using the same view but in your index() method you are not providing any data to the view so you get the error.

Dunsti

as far as I know, the ->with() - method is not able to pass arrays to your view. It is only for single values.

see here: https://laravel.com/docs/5.5/views#passing-data-to-views

Try this syntax:

return view('AdminPanel', compact('data'));
tykus
tykus
3 months ago (521,110 XP)
biishmar
return view('AdminPanel')->with(['data',$data]);
arthurvillar

Please follow the series Laravel 5.4 From Scratch. Looks like you need to learn the basics and threads like this will only get you more confused!

Kistlak

@skliche - Can demonstrate it plz ??

@arthurvillar - okay , I will check out..

Please sign in or create an account to participate in this conversation.