简体   繁体   English

Laravel 4-如何从哪里访问外部变量

[英]Laravel 4 - How to access outside variables from an advance where

I have a scope method in my model named Book. 我的模型中有一个名为Book的范围方法。

public function scopeBookAuthor($query, $input = array()){
    if($input['book_author'] != ''){
        return $query->where(function ($query) {
                    $query->where('book_author_last_name', 'LIKE', "%".$input['book_author']."%")
                          ->orWhere('book_author_middle_name', 'LIKE', "%".$input['book_author']."%")
                          ->orWhere('book_author_first_name', 'LIKE', "%".$input['book_author']."%");
                });

    }
}

An error occurred inside the function in the 3rd line. 第三行中的函数内部发生错误。 it says Undefined variable: input . 它说Undefined variable: input

I tried to include the input variable as another parameter but it didn't work 我试图将输入变量作为另一个参数包括在内,但没有用

return $query->where(function ($query, $input) {...

Is there a way to make this possible? 有没有办法使之成为可能? thanks in advance. 提前致谢。

有一个肮脏的hack:

return $query->where(function ($query) use ($input){

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM