2014-06-26 133 views
0

我是PHP新手。如何将范围变量传递给匿名函数

我想创建一个函数链接这个。

public static function cat_post($category, $limit, $top) 
{ 
    $posts = Post::whereHas('categories', function($q) 
     { 
      $q->where('name', 'like', $name); 
      $q->where('top', 'like', $top); 

     })->take($limit)->get(); 
} 

,但我得到

Undefined variable "name" 

请帮助我。如何创建这个功能....

回答

1

使用如下:

public static function cat_post($category, $limit, $top) 
{ 
    $posts = Post::whereHas('categories', function($q) use ($name, $top) 
     { 
      $q->where('name', 'like', $name); 
      $q->where('top', 'like', $top); 

     })->take($limit)->get(); 
} 

看看here

相关问题