Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

schroedingerswg's avatar

Pass var into function

Hey there, I'm stuck currently passing a variable into a function. Problem seemed so basic that I didn't wanted to ask. But since I wasn't able to find a solution online, her the problem:

 37   function addItem(\App\Rental $rental) {
 38 
 39         $rental=\App\Rental::findOrFail($rental->id);
 40 
 41         $request=request();
 42 
 43         $validator=\Validator::make(request()->all(),[
 44             "item_id"=>"min:1|max:2000|required|numeric|exists:items,id"
 45         ]);
 46                                                                                                                    
 47         $validator->after(function($validator) {
 48             if($rental->hasItem(request("item_id")))
 49                $validator->errors()->add('item_id', 'Item is already on the list');
 50         });    
 51 

I get an error in line 48, that $rental is unknown.

Is there a way to pass $rental into the function?

Thank you very much!

0 likes
1 reply
schroedingerswg's avatar

And whats the advantage of $rental=\App\Rental::findOrFail($rental->id);

Since $rental is already some sort of \App\Rental but not really ?!?

... sorry, I'm lost :D

Please or to participate in this conversation.