Laravel 5.7 Undefined variable in view

laravel undefined variable in view
undefined variable in laravel blade
undefined variable laravel controller
undefined variable message laravel
undefined variable: id laravel
undefined variable php
errorexception undefined variable
undefined variable: data

I follow the guide ( https://www.phpflow.com/php/laravel-5-6-crud-operation-using-resource-controller/ ) and at point "How To Create Listing in Laravel 5.6" I get the error::

ErrorException (E_ERROR) Undefined variable: employees (View: C:\xampp\htdocs\crud\resources\views\pages\index.blade.php)

Previous exceptions * Undefined variable: employees (0)

And in code window the error is:

<?php 
$__currentLoopData = $employees;
$__env->addLoop($__currentLoopData);
foreach($__currentLoopData as $key => $emp): 
    $__env->incrementLoopIndices();
    $loop = $__env->getLastLoop(); ?>

Is it a compatibility issue between 5.6 and 5.7 or what? (please note that I am a noob in Laravel)

The guide is pretty slim, what you need to do in order to get you index working:

namespace App\Http\Controllers;

use App\Employee;
use Illuminate\Http\Request;

class EmployeeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        return view('pages.index', ['employees' => Employee::all()]);
    }

    // ... The rest of the controller method below
}

If your resource definition is:

Route::resource('employee', 'EmployeeController');

The Path (URL) to access this will be: /employee

Laravel 5.7 Undefined variable in view, The guide is pretty slim, what you need to do in order to get you index working: namespace App\Http\Controllers; use App\Employee; use  I can not get the variable from the controller, if someone can help me will be great. Im working with PHP Laravel 5.7.2. DetectController.php: public function

According to your link I do not see the full code of the controller, but your index method should look like this

public function index()
{
    $employees = Employee::all();

    // Pass data to view
    return view('employee.index', ['employees' => $employees]);
}

How to fix Undefined Variable Error in Laravel, How to fix Undefined Variable Error in Laravel. 8.5K views. 17. 41. Share. Save. Report Duration: 2:10 Posted: Sep 6, 2018 This works in a 5.7 but currently updating it to 6.2 version Undefined variable: user (View: C:\xampp\htdocs\laravel\project_path\resources\views\emails\blogPosts\commented.blade.php) Here is the controller

The error remains, part of my code so far, EmployeeController.php:

public function index()
{
$employees = Employee::all();
return view('employee.index', ['employees' => $employees]);
}

Employee.php

class Employee extends Model
{
// Table name
protected $table = 'crud';
// Primary key
public $primaryKey = 'id';
}

index.blade.php

<tbody>
    @foreach($employees as $key => $emp)
    <tr>
        <td>{{ $emp->id }}</td>
    </tr>
    @endforeach
</tbody>

Undefined variable: data in view Laravel., 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  I am getting this error: undefined variable. I read a lot of posts about it but, none of them helped with the problem i am facing. @tykus here's what

Undefined Variable in Blade, Laravel 5.7, I get this error: ErrorException (E_ERROR) Undefined Undefined Variable in Blade You can pass one array only in the view: FYI - This is the first thing I've built myself in Laravel so I know my code wont be perfect or anything like that, I'm 100% sure there are a million better ways to do what I'm doing I just haven't found them yet.

Undefined variable: students (View: D:\exam\curd\resources\views , working with laravel 5.7 and need display student table data in the home page, home.blade.php <table class="table"> If the compiled view either does not exist, or the uncompiled view has been modified, Laravel will recompile the view. Compiling views during the request negatively impacts performance, so Laravel provides the view:cache Artisan command to precompile all of the views utilized by your application. For increased performance, you may wish to run

compact with undefined variable breaks on PHP 7.3 · Issue #26936 , ErrorException compact(): Undefined variable: op. Likely this is fixed in 5.5(​since it is an LTS version) and the current version, 5.7. ~CloudDocs/Developer/​PHP/fdf/vendor/laravel/framework/src/Illuminate/View/Middleware/  {note} Views rendered via @each do not inherit the variables from the parent view. If the child view requires these variables, you should use @foreach and @include instead. Stacks. Blade allows you to push to named stacks which can be rendered somewhere else in another view or layout.

Comments
  • Can you please show your controller code?
  • The error is pretty straightforward, $employees is missing. Make sure you passed it through to your view. And that you don't have a typo, 'employee' => $employees
  • Are you passing the employee variable to the view?
  • You should be returning pages.index not employee.index
  • You are correct, I changed it to pages.index but the error remains the same. I also tried return view('pages.index')->with('employees', $employees);, but nothing changed.
  • try clearing your cached views. Also, what does your route look like? And what url are you accessing in your browser?
  • When I delete the code between <tbody></tbody> tags then the page loads fine. The web.php has 2 lines of code Route::get('/', 'PagesController@index'); Route::resource('employee', 'EmployeeController'); .
  • And what url are you hitting in your browser?