2014-05-09 57 views
1

嘗試幾分鐘後。我有這個在我的routes.php文件文件Laravel 4:ErrorException未定義變量

Route::get('home/bookappointment/{id?}', array('as' => 'bookappointment', 'uses' => '[email protected]')); 

而且我控制器上我有以下幾點:

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); 
} 

而且我不斷收到此錯誤。 enter image description here

我在做什麼錯?

+0

未定義變量通常在未定義變量時運行。 –

+0

但如果我通過我的網址發送這個? home/bookappointment/297 – Monica

回答

11

您無權在您的匿名功能中使用$id。嘗試

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

在附加信息的PHP文件看看:http://www.php.net/manual/en/functions.anonymous.php

+1

謝謝!我愛你!!!有效!! – Monica

+1

@Monica非常普遍的問題。它每天都在發生! –

1

在你的路由文件{id後的問號表示ID是可選的。如果確實應該是可選的,那麼它應該在控制器中被定義爲可選的。

採取下面的例子:

class FrontController extends BaseController { 

    public function getBookAppointment($id = null) 
    { 
     if (!isset($id)) 
     { 
      return Response::make('You did not pass any id'); 
     } 
     return Response::make('You passed id ' . $id); 
    } 
} 

注意該方法如何與$id參數作爲= null定義。這樣你可以允許可選參數。

+0

謝謝,只是刪除了問號,因爲我需要這個ID。 – Monica

相關問題