doulan8330 2014-06-26 05:02
浏览 49
已采纳

如何将范围变量传递给匿名函数

I am new in PHP.

I want to create a function link this.

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();
}

But i got

Undefined variable "name"

Please help me. How to create this function....

  • 写回答

1条回答 默认 最新

  • dt246813579 2014-06-26 05:03
    关注

    use as below:

    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();
    }
    

    have a look here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?