Kistlak
34
25
Laravel

Undefined variable: data in view.

Posted 8 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]');

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

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.