查询构建器无法识别whereIn

时间:2015-01-20 14:39:58

标签: mysql sql laravel query-builder

使用查询构建器查询数据库时,出现错误

    Object of class Closure could not be converted to string 

        // Each type of where clauses has its own compiler function     which is responsible
        // for actually creating the where clauses SQL. This helps keep the code nice
       // and maintainable since each clause has a very small method that it uses.
       foreach ($query->wheres as $where)
       {
          $method = "where{$where['type']}";
          $sql[] = $where['boolean'].' '.$this->$method($query, $where);
       }

这是造成错误的野兽

     DB::table('questions')
                    ->join('question_topic', 'question_topic.question_id', '=', 'questions.id')
                    ->join('topics', 'topics.id', '=', 'question_topic.topic_id')
                    ->join('chapters', 'chapters.id', '=', 'topics.chapter_id')
                    ->join('subjects', 'subjects.id', '=', 'chapters.subject_id')
                    ->whereIn('questions.teacher_id', '=', function($query) use ($school_id)
                            {
                                $query->select('user_id')
                                      ->from('schoolmember_school')
                                      ->where('school_id', '=', $school_id);
                            })
                    ->select('questions.id', 'questions.question', 
                             'topics.id as topic_id', 'chapters.name as chapter_name', 
                             'chapters.class_id', 'subjects.name as subject_name',
                             'topics.name as topic_name')
                    ->get();

错误:

ErrorException thrown with message "Object of class Closure could not be converted to string"

Stacktrace:
#26 ErrorException in A:\xampp\htdocs\learnsmart\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php:202
#25 Illuminate\Exception\Handler:handleError in A:\xampp\htdocs\learnsmart\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php:202
#24 Illuminate\Database\Query\Grammars\Grammar:compileWheres in A:\xampp\htdocs\learnsmart\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php:60
#23 Illuminate\Database\Query\Grammars\Grammar:compileComponents in A:\xampp\htdocs\learnsmart\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php:38
#22 Illuminate\Database\Query\Grammars\Grammar:compileSelect in A:\xampp\htdocs\learnsmart\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\MySqlGrammar.php:34
#21 Illuminate\Database\Query\Grammars\MySqlGrammar:compileSelect in A:\xampp\htdocs\learnsmart\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php:1234
#20 Illuminate\Database\Query\Builder:toSql in A:\xampp\htdocs\learnsmart\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php:1359
#19 Illuminate\Database\Query\Builder:runSelect in A:\xampp\htdocs\learnsmart\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php:1349
#18 Illuminate\Database\Query\Builder:getFresh in A:\xampp\htdocs\learnsmart\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php:1336
#17 Illuminate\Database\Query\Builder:get in A:\xampp\htdocs\learnsmart\app\controllers\TeacherController.php:187
#16 TeacherController:school_questions in <#unknown>:0
#15 call_user_func_array in A:\xampp\htdocs\learnsmart\vendor\laravel\framework\src\Illuminate\Routing\Controller.php:231
#14 Illuminate\Routing\Controller:callAction in A:\xampp\htdocs\learnsmart\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php:93
#13 Illuminate\Routing\ControllerDispatcher:call in A:\xampp\htdocs\learnsmart\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php:62
#12 Illuminate\Routing\ControllerDispatcher:dispatch in A:\xampp\htdocs\learnsmart\vendor\laravel\framework\src\Illuminate\Routing\Router.php:962
#11 Illuminate\Routing\Router:Illuminate\Routing\{closure} in <#unknown>:0
#10 call_user_func_array in A:\xampp\htdocs\learnsmart\vendor\laravel\framework\src\Illuminate\Routing\Route.php:109
#9 Illuminate\Routing\Route:run in A:\xampp\htdocs\learnsmart\vendor\laravel\framework\src\Illuminate\Routing\Router.php:1028
#8 Illuminate\Routing\Router:dispatchToRoute in A:\xampp\htdocs\learnsmart\vendor\laravel\framework\src\Illuminate\Routing\Router.php:996
#7 Illuminate\Routing\Router:dispatch in A:\xampp\htdocs\learnsmart\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:775
#6 Illuminate\Foundation\Application:dispatch in A:\xampp\htdocs\learnsmart\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:745
#5 Illuminate\Foundation\Application:handle in A:\xampp\htdocs\learnsmart\vendor\laravel\framework\src\Illuminate\Session\Middleware.php:72
#4 Illuminate\Session\Middleware:handle in A:\xampp\htdocs\learnsmart\vendor\laravel\framework\src\Illuminate\Cookie\Queue.php:47
#3 Illuminate\Cookie\Queue:handle in A:\xampp\htdocs\learnsmart\vendor\laravel\framework\src\Illuminate\Cookie\Guard.php:51
#2 Illuminate\Cookie\Guard:handle in A:\xampp\htdocs\learnsmart\vendor\stack\builder\src\Stack\StackedHttpKernel.php:23
#1 Stack\StackedHttpKernel:handle in A:\xampp\htdocs\learnsmart\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:641
#0 Illuminate\Foundation\Application:run in A:\xampp\htdocs\learnsmart\public\index.php:49

1 个答案:

答案 0 :(得分:1)

whereIn()没有运营商!第二个参数应该是闭包:

->whereIn('questions.teacher_id', function($query) use ($school_id){ /* ... */})