Using a varible in a where clause

Using a varible in a where clause

Twiggy21Twiggy21 Posts: 16Questions: 6Answers: 0

Hi There,

I am trying to use a variable in my where clause:

->where( function ( $q ) {
$q
  ->where('primary_profile.nid', 39);

} )

I simply want to change 39 to be a variable but when I change 39 to:

->where( function ( $q ) {
$q
  ->where('primary_profile.nid', $nid);

} )

I get an error. I read a bit about SSP but the documentation makes no sense and there is no clear example. It seems this should be relativity straight forward but it has eaten up half a day. Can anyone let me know what I am missing or point me to a example that spells this put plainly.

Thanks in advance,

Replies

  • allanallan Posts: 64,943Questions: 1Answers: 10,757 Site admin

    Yeah - this is PHP's werido closure syntax. Because they don't really do global variables you need to use the use statement:

    ->where( function ( $q ) use ( $nid ) {
    $q
      ->where('primary_profile.nid', $nid);
    

    More about that in the PHP documentation.

    Allan

This discussion has been closed.