2014-05-09 5 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

하지만 내 URL을 통해 보내는 경우 ?? 집/예약/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가 옵션을 나타냅니다. 실제로 옵션이어야하는 경우 컨트롤러에서 선택적으로 정의해야합니다. 방법이 = null으로 $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); 
    } 
} 

참고 :

는 다음을 예로 들어 보겠습니다. 이 방법을 사용하면 선택적 인수를 허용 할 수 있습니다.

+0

감사합니다. 그냥 물음표를 제거 했으므로 ID가 필요합니다. – Monica

관련 문제