Laravel 4:ErrorException未定义的变量

Mon*_*ica 2 laravel eloquent laravel-4

试了几分钟后.我的routes.php文件中有这个

Route::get('home/bookappointment/{id?}', array('as' => 'bookappointment', 'uses' => 'FrontController@getBookAppointment'));
Run Code Online (Sandbox Code Playgroud)

在我的控制器上,我有以下内容:

    public function getBookAppointment($id)
{
        // find all the services that are active for the especific franchise
        $activeServices = Service::whereHas('franchise', function($q) {
                $q->where('franchise_id', '=', $id);
            })->get();

        //var_dump($activeServices);die;

        return View::make('frontend/bookappointment')
                                ->with('services', $activeServices);
}
Run Code Online (Sandbox Code Playgroud)

而且我一直收到这个错误. 在此输入图像描述

我究竟做错了什么?

小智 11

您无权访问$id匿名函数.尝试

<?php
$activeServices = Service::whereHas('franchise', function($q) use($id) {
    $q->where('franchise_id', '=', $id);
})->get();
Run Code Online (Sandbox Code Playgroud)

有关其他信息,请查看PHP文档:http://www.php.net/manual/en/functions.anonymous.php