Alireza Heydari
Alireza Heydari

Reputation: 1

how can pass function as argument php class

I want to write a class to submit using the function method and get access to example_method at the class

$users = User::where('posts', function($q){
    $q->example_method ('created_at', '>=', '2015-01-01 00:00:00');
})->get() ;

Upvotes: 0

Views: 51

Answers (1)

Chrzanek
Chrzanek

Reputation: 197

As I understand - you want to have access to $q variable in anonymous function, you can access it this way:

$users = User::where('posts', function() use ($q) {
    $q->example_method ('created_at', '>=', '2015-01-01 00:00:00');
})->get() ;

In case you want access variable defined outside function you have to inherit it using use.

More info: https://www.php.net/manual/en/functions.anonymous.php

Upvotes: 1

Related Questions