简体   繁体   中英

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

I have a scope method in my model named 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 .

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){

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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