2017-03-16 2 views
2

저는 Laravel의 초보자입니다. 그래서 모든 것이 탐사 기간에 있습니다. 나는 내가 $ 요청이 정의되어있어laravel 요청은 정의되지 않았습니다.

$request->input('key') 

를 사용하는 경우 나, 그러나

dd($request) 

Request {#40 
    #json: ParameterBag {#32 
    #parameters: array:4 [ 
     "GPTour_id" => 1 
     "customer_id" => 1 
     "status" => "Confirmed" 
     "note" => "asdfasdf" 
    ] 
    } 
    #userResolver: Closure {#300 
    class: "Illuminate\Auth\AuthServiceProvider" 
    this: AuthServiceProvider {#22 …} 
    use: array:1 [ 
     "$app" => Application {#3 
     #basePath: "/Users/haophung/Dropbox/server/websites/nglaravelyep/laravel-backend" 
     #hasBeenBootstrapped: true 
     #booted: true 
     #bootingCallbacks: [] 

수 있어요 laravel에 걸쳐 및 laravel 컨트롤러에 데이터를 전송하기 위해 각 HTTP 포스트를 사용합니다. 제발 조언! GospelController.php 라인 (60)에

public function addGospelCustomer(Request $request) 
    { 
     if ($request) { 
      $customer_id = $request->get('customer_id'); 
      $tour_id = $request->get('GPTour_id'); 
      $validator = Validator::make($request->all(), [ 
       'customer_id' =>'required' 
      ]); 

      if ($validator->fails()) { 
       return response()->json(['error' => $validator->errors()], 406); 
      } 
      $gospel_customer = Gospel_tour::find($tour_id)->with(['customers' => function($query) { 
       $query->where('id', $customer_id); 
      }])->first(); 

      if ($gospel_customer === 'null') { 
       return response()->json(['error' => "The Customer is already on the list"], 406); 
      } 

      return 'success';//response()->json(['success' => $request], 200); 
     }else { 
      return response()->json(['error' =>'can not add customer'], 401); 
     } 
    } 

ErrorException : 정의되지 않은 변수는 : 나는 문제가

$gospel_customer = Gospel_tour::find($tour_id)->with(['customers' => function($query) { 
     $query->where('id', $customer_id); 
    }])->first(); 

내가 $ 밖으로 CUSTOMER_ID 에코 할 수있다 생각하지만,이 웅변에 정의되지 않은

을 CUSTOMER_ID

+0

'$ request-> get ("key"); ' 어때요? –

+0

내 업데이트를 확인하여 오류를 수정하십시오. i dd ($ request-> get ('key'))에 값이있는 경우'use ($ customer_id)' – EddyTheDove

+0

을 추가해야합니다. var에 할당 된 경우 왜 작동하지 않는지 모르겠습니다 .. –

답변

1

함수 정의에 힌트 요청을 입력해야합니다.

public function name(Request $request) {} 

그리고

$gospel_customer = Gospel_tour::find($tour_id)->with(['customers' => function($query) use ($customer_id) { 
    $query->where('id', $customer_id); 
}]); 
012을

$key = $request->key; 
$key = $request->get('key'); 

처럼 사용 또는 전역 함수

$key = request('key'); 

에게 오류 예외가 업데이트

를 사용

클로저 안에 있기 때문에 오류가 발생하며 외부 변수에 액세스 할 수 없습니다.

+0

@EddyTheDove –

+0

을 타이핑 했는데도 여전히 작동하지 않습니까? – EddyTheDove

+0

@EddyTheDove가 작동하지 않습니다 –

관련 문제