Query Designer: Passing an Anonymous Function Argument

I had a problem with passing a variable to close the request, here is my code:

function get_usersbyname($name){ dd($name); $resultset = DB::table('users')->where(function($query){ $query->where('username', 'LIKE', $name); }); .... } 

if I run it, it returns the error " undefined name variable ", but I already passed the variable $name and checked its existence. Also, I cannot find any resource that explains how to pass a variable to an anonymous request function. Could you help me with this problem?

+7
source share
1 answer

You need to tell an anonymous function to use this variable, for example ...

Since this variable is outside the scope of the anonymous function, it must be passed using the use keyword, as shown in the example below.

 function get_usersbyname($name){ dd($name); $resultset = DB::table('users')->where(function($query) use ($name) { $query->where('username', 'LIKE', $name); }); .... } 
+28
source

All Articles