3

I am a newbie in Laravel, so everything is under exploration period. I use angular http post to send data over to laravel and in laravel controller i am able to

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: [] 

However, if i use

$request->input('key') 

i got $request is undefined. Please advise!!!

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 in GospelController.php line 60: Undefined variable: customer_id

I think the problem is

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

I can echo $customer_id out, but in this eloquent is not defined

4
  • $request->get("key"); how about? Commented Mar 16, 2017 at 1:01
  • Check my update to fix your error. You need to add use($customer_id) Commented Mar 16, 2017 at 1:20
  • if i dd($request->get('key') i got the value. i don't know why if assigned to a var it doesn't work. Commented Mar 16, 2017 at 1:20
  • Just check my update for a fix. Commented Mar 16, 2017 at 1:27

1 Answer 1

3

You need to typehint requestion in your function definition

public function name(Request $request) {} 

And use it like

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

Or use the global function

$key = request('key'); 

Update

Where you have the error exception do

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

The error occurs because you are inside a closure, and it doesn't have access to external variables.

Sign up to request clarification or add additional context in comments.

2 Comments

I did typehing @EddyTheDove
Even with the global helper? Please add your controller to your question and let's see.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.