Passing a variable in a where clause

Passing a variable in a where clause

lougallilougalli Posts: 1Questions: 1Answers: 0

Trying to pass a variable to a where clause and continue to get JSON errors

This works fine -

->where( function ($q) {
$q->where( 'CONTACT_ID', 5 );
} )

This does not -
->where( function ($q) {
$q->where( 'CONTACT_ID', $varContactID );
} )

Answers

  • rf1234rf1234 Posts: 2,957Questions: 87Answers: 417

    Well, you are not passing it in I am afraid. And I guess you can't. But you can use global PHP variables like $_SESSION ... Those will work.

  • allanallan Posts: 63,356Questions: 1Answers: 10,447 Site admin

    This is how PHP antonymous functions work. You have to use the use statement to be able to access the parameter:

    ->where( function ($q) use ($varContactID) {
    

    See the PHP documentation for details.

    I'll probably write an FAQ about this as it keeps coming up, even although its a PHP issue rather than specifically DataTables.

    Allan

This discussion has been closed.