Back

Undefined variable: data in view Laravel.


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 route

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

Chris Schofield replied 3 months ago

Hi!

It's odd that it's not passing the data. Instead of:

return view('AdminPanel')->with('data',$data);

try

return view('AdminPanel',['data'=>$data]);

And post your results.

Tobias van Beek replied 3 months ago

Your index action returns the same view but without any data, so I suspect that you visit /AdminPanel and get this error.

If you visit /AdminView you should get the page you want to see.

Kistlak Rajapaksh replied 3 months ago

@Chris - I have updated my Controller and routes as below. I didn't change View file and I still get same error as this - "Undefined variable: data (View: D:\wamp64\www\FinalProject\resources\views\AdminPanel.blade.php)"

AdminPanelController.php

<?php

namespace App\Http\Controllers;

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

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

Route

Route::get('/AdminPanel', '[email protected]')->name('AdminPanel');
Warlord replied 3 months ago Solution

Try making a simple test and just pass a simple value instead of a model.

eg.

    public function index()
    {
        $data = 'this is a test';
        return view('AdminPanel', compact(['data']));
    }
    
...8<...

Then simplify your view so it just shows $data

{{ $data }}

Then once you establish you're passing data you can change it to the model. Make sure $data is set before calling the view by adding in a dd($data) so you can be sure it is set.


Sign in to participate in this thread!


We'd like to thank these amazing companies for supporting us