1

I'm unable to pass my controller variable $sickLeaves to my view, which is reached via index.blade.php of my SickLeaveController (which is a RESTful resource controller). I keep getting the error: Undefined variable 'sickLeaves'

Here is the routes.php:

Route::group(['middleware' => 'web'], function(){
    Route::auth();
    Route::get('/', 'HomeController@index');
    Route::get('auth/logout', 'Auth\AuthController@getLogout');
    Route::get('/home', 'HomeController@index');
    Route::resource('sickleaves','SickLeaveController');
});

Here is my SickLeaveController:

class SickLeaveController extends Controller
{

public function index()
{

    $sickLeaves = SickLeave::all();

    return view('sickleaves.index')->with('sickleaves',$sickLeaves);
}


public function create()
{
    $users = User::pluck('name','id');

    return View::make('sickleaves.create')->with('sickleaves',$users);
}


public function store(Request $request)
{
    $this->validate($request, [
        'firstname' => 'required|max:255',
        'lastname' => 'required|max:255',
        'reason' => 'required|max:255',
        'startdate' => 'required',
        'status' => 'required',]);
}


public function show($id)
{
    $sickLeave = SickLeave::find($id);

    return View::make('sickleaves.show')->with('sickleaves',$sickLeave);
}


public function edit($id)
{
    $sickLeave = SickLeave::findOrFail($id);

    return View::make('sickleaves.edit')->with('sickleaves',$sickLeave);
}


public function update(Request $request, $id)
{
    //TODO
}


public function destroy($id)
{
    $sickLeave = SickLeave::findOrFail($id);
    $sickLeave->delete();

    Session::flash('Item successfully deleted');
    return redirect()->route('sickleave.index');
}
}

Here is the index.blade.php:

@foreach($sickLeaves as $sickLeave)
                <tr>
                    <td style="width: 8%">{{ $sickLeave->id }}</td>
                    <td style="width: 12%"><span class="fw-semi-bold">{{ $sickLeave->firstname }}</span></td>
                    <td style="width: 12%"><span class="fw-semi-bold">{{ $sickLeave->surname }}</span></td>
                    <td class="no-sort hidden-xs" style="width: 12%">{{ $sickLeave->reason }}</td>
                    <td class="hidden-xs">{{ $sickLeave->startdate }}</td>
                    <td class="hidden-xs">{{ $sickLeave->enddate }}</td>
                    <td class="no-sort">{{ $sickLeave->status }}</td>
                    <td class="no-sort"><div class="btn-group">
                            <button class="btn btn-inverse">Actions</button>
                            <button class="btn btn-inverse dropdown-toggle" data-toggle="dropdown">
                                <i class="fa fa-caret-down"></i>
                            </button>
                            <ul class="dropdown-menu">
                                <li><a href="{{ route('sickleaves.show', $sickLeave->id) }}">View Details</a></li>
                                <li><a href="{{ route('sickleaves.edit', $sickLeave->id) }}">Edit</a></li>
                                <li class="divider"></li>
                                <li><a href="#">Delete</a></li>
                            </ul>
                        </div>
                    </td>
                </tr>
                @endforeach
tereško
  • 58,060
  • 25
  • 98
  • 150
omrakhur
  • 1,362
  • 2
  • 24
  • 48

2 Answers2

2

Use correct variable:

@foreach($sickleaves as $sickLeave)

Also, remove web middleware if you're using 5.2.27 or later.

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • I am using 5.2.31, but there was a post on laracasts that said that we have to include it, which is why I put it there, otherwise I had it removed. wrapping it around web middleware didn't solve the problem though. https://laracasts.com/discuss/channels/laravel/errorexception-undefined-variable-errors – omrakhur May 13 '16 at 15:17
  • Sorry, I didn't choose your answer as the best one, because the first answer cleared one of my doubts about the with() function. Your answer could be taken as correct as well, so I upvoted it. – omrakhur May 13 '16 at 15:24
  • 1
    @omrakhur, about `web` middleware. If you're using 5.2.27 and higher, you should remove it. However, if you did upgrade from <5.2.27 to a higher version, you should check `app/Providers/RouteServiceProvider.php` file and if it doesn't add `web` middleware, you should add it manually. Please check this answer: http://stackoverflow.com/a/36784408/1227923 – Alexey Mezenin May 13 '16 at 15:32
1

PHP variables are case sensitive!

You passing sickleaves, but in view You are trying to get sickLeaves, that is the problem.

Giedrius Kiršys
  • 5,154
  • 2
  • 20
  • 28
  • Thank you! Previously someone had told me that the first argument for the with() function has to be the same as the route. Which sounded very confusing. I think with(arg1, arg2), copies the content from arg2 to arg1? Is that what's going on here? – omrakhur May 13 '16 at 15:23
  • 1
    @omrakhur No, first argument for with don't have to be same as the route. It's just a key which will hold Your payload from second parameter. Also You can consider to not use `->with()`, but instead pass data like so: `view('yourView', $data)` where `$data` is an array of items to pass to view. For example `view('sickleaves.index', ['sickleaves' => $sickLeaves])`. Also have a look at http://php.net/manual/en/function.compact.php. It comes handy when You don't need to change variable name. – Giedrius Kiršys May 13 '16 at 16:08
  • I have used compact with other PHP frameworks, so might give it a try. – omrakhur May 16 '16 at 08:12