在将变量从控制器传递到视图时,在Laravel 5中获取未定义的变量

时间:2016-05-13 14:58:56

标签: php twitter-bootstrap laravel

我无法将我的控制器变量$ sickLeaves传递给我的视图,这是通过我的SickLeaveController(它是一个RESTful资源控制器)的index.blade.php到达的。 我一直收到错误:未定义的变量' sickLeaves'

这是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');
});

这是我的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');
}
}

这是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

2 个答案:

答案 0 :(得分:2)

使用正确的变量:

@foreach($sickleaves as $sickLeave)

此外,如果您使用的是5.2.27或更高版本,请删除web中间件。

答案 1 :(得分:1)

PHP变量区分大小写!

您传递的是sickleaves,但在视图中您正试图获取sickLeaves,这就是问题所在。