我无法对我现在遇到的问题做出正面或反面但现在正在发生的事情,我对Eloquent lists
方法存在问题。
以下一行:
dd(Quiz::lists('name', 'id')->all());
返回此错误:
ErrorException: explode() expects parameter 2 to be string, array given
堆栈跟踪似乎并没有给我很多工作:
[2015-11-10 10:48:17] testing.ERROR: exception 'ErrorException' with message 'explode() expects parameter 2 to be string, array given' in /home/vagrant/.composer/vendor/illuminate/support/helpers.php:404
Stack trace:
#0 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'explode() expec...', '/home/vagrant/....', 404, Array)
#1 /home/vagrant/.composer/vendor/illuminate/support/helpers.php(404): explode('.', Array)
#2 /home/vagrant/Web/Laravel/careers_app_server/vendor/laravel/framework/src/Illuminate/Support/Arr.php(319): data_get(Object(stdClass), Array)
#3 /home/vagrant/Web/Laravel/careers_app_server/vendor/laravel/framework/src/Illuminate/Support/Collection.php(428): Illuminate\Support\Arr::pluck(Array, 'name', 'id')
#4 /home/vagrant/Web/Laravel/careers_app_server/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(1572): Illuminate\Support\Collection->pluck('name', 'id')
#5 /home/vagrant/Web/Laravel/careers_app_server/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(244): Illuminate\Database\Query\Builder->lists('name', 'id')
#6 [internal function]: Illuminate\Database\Eloquent\Builder->lists('name', 'id')
#7 /home/vagrant/Web/Laravel/careers_app_server/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(3482): call_user_func_array(Array, Array)
#8 [internal function]: Illuminate\Database\Eloquent\Model->__call('lists', Array)
#9 [internal function]: App\Quiz->lists('name', 'id')
#10 /home/vagrant/Web/Laravel/careers_app_server/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(3496): call_user_func_array(Array, Array)
#11 /home/vagrant/Web/Laravel/careers_app_server/app/Http/Controllers/QuizController.php(21): Illuminate\Database\Eloquent\Model::__callStatic('lists', Array)
#12 /home/vagrant/Web/Laravel/careers_app_server/app/Http/Controllers/QuizController.php(21): App\Quiz::lists('name', 'id')
#13 [internal function]: App\Http\Controllers\QuizController->index()
#14 /home/vagrant/Web/Laravel/careers_app_server/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(256): call_user_func_array(Array, Array)
#15 /home/vagrant/Web/Laravel/careers_app_server/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(164): Illuminate\Routing\Controller->callAction('index', Array)
#16 /home/vagrant/Web/Laravel/careers_app_server/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(112): Illuminate\Routing\ControllerDispatcher->call(Object(App\Http\Controllers\QuizController), Object(Illuminate\Routing\Route), 'index')
#17 [internal function]: Illuminate\Routing\ControllerDispatcher->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#18 /home/vagrant/Web/Laravel/careers_app_server/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(139): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#19 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#20 /home/vagrant/Web/Laravel/careers_app_server/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#21 /home/vagrant/Web/Laravel/careers_app_server/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(114): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#22 /home/vagrant/Web/Laravel/careers_app_server/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(69): Illuminate\Routing\ControllerDispatcher->callWithinStack(Object(App\Http\Controllers\QuizController), Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request), 'index')
#23 /home/vagrant/Web/Laravel/careers_app_server/vendor/laravel/framework/src/Illuminate/Routing/Route.php(203): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request), 'App\\Http\\Contro...', 'index')
#24 /home/vagrant/Web/Laravel/careers_app_server/vendor/laravel/framework/src/Illuminate/Routing/Route.php(134): Illuminate\Routing\Route->runWithCustomDispatcher(Object(Illuminate\Http\Request))
#25 /home/vagrant/Web/Laravel/careers_app_server/vendor/laravel/framework/src/Illuminate/Routing/Router.php(704): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request))
#26 [internal function]: Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#27 /home/vagrant/Web/Laravel/careers_app_server/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(139): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#28 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#29 /home/vagrant/Web/Laravel/careers_app_server/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#30 /home/vagrant/Web/Laravel/careers_app_server/vendor/laravel/framework/src/Illuminate/Routing/Router.php(706): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#31 /home/vagrant/Web/Laravel/careers_app_server/vendor/laravel/framework/src/Illuminate/Routing/Router.php(671): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#32 /home/vagrant/Web/Laravel/careers_app_server/vendor/laravel/framework/src/Illuminate/Routing/Router.php(631): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#33 /home/vagrant/Web/Laravel/careers_app_server/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(236): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#34 [internal function]: Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#35 /home/vagrant/Web/Laravel/careers_app_server/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(139): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#36 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#37 /home/vagrant/Web/Laravel/careers_app_server/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#38 /home/vagrant/Web/Laravel/careers_app_server/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(122): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#39 /home/vagrant/Web/Laravel/careers_app_server/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(87): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#40 /home/vagrant/Web/Laravel/careers_app_server/vendor/laravel/framework/src/Illuminate/Foundation/Testing/CrawlerTrait.php(393): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#41 /home/vagrant/Web/Laravel/careers_app_server/vendor/laravel/framework/src/Illuminate/Foundation/Testing/CrawlerTrait.php(70): Illuminate\Foundation\Testing\TestCase->call('GET', 'api/quiz', Array, Array, Array, Array)
#42 /home/vagrant/Web/Laravel/careers_app_server/tests/QuizTest.php(27): Illuminate\Foundation\Testing\TestCase->get('api/quiz')
#43 [internal function]: QuizTest->testRequestQuizList()
#44 /home/vagrant/Web/Laravel/careers_app_server/vendor/phpunit/phpunit/src/Framework/TestCase.php(909): ReflectionMethod->invokeArgs(Object(QuizTest), Array)
#45 /home/vagrant/Web/Laravel/careers_app_server/vendor/phpunit/phpunit/src/Framework/TestCase.php(768): PHPUnit_Framework_TestCase->runTest()
#46 /home/vagrant/Web/Laravel/careers_app_server/vendor/phpunit/phpunit/src/Framework/TestResult.php(612): PHPUnit_Framework_TestCase->runBare()
#47 /home/vagrant/Web/Laravel/careers_app_server/vendor/phpunit/phpunit/src/Framework/TestCase.php(724): PHPUnit_Framework_TestResult->run(Object(QuizTest))
#48 /home/vagrant/Web/Laravel/careers_app_server/vendor/phpunit/phpunit/src/Framework/TestSuite.php(747): PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))
#49 /home/vagrant/Web/Laravel/careers_app_server/vendor/phpunit/phpunit/src/Framework/TestSuite.php(747): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#50 /home/vagrant/Web/Laravel/careers_app_server/vendor/phpunit/phpunit/src/TextUI/TestRunner.php(440): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#51 /home/vagrant/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php(154): PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Framework_TestSuite), Array)
#52 /home/vagrant/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php(105): PHPUnit_TextUI_Command->run(Array, true)
#53 /home/vagrant/.composer/vendor/phpunit/phpunit/phpunit(47): PHPUnit_TextUI_Command::main()
#54 {main}
我已经用多种不同型号测试了这个问题,问题仍然存在。它可能与环境有关吗?使用PHPUnit运行测试时会出现此错误,我还没有在非测试环境中尝试过。我会更新帖子。
一些信息:
Laravel版本:5.1.11 服务器:Homestead默认
经过一些测试后,我发现问题只发生在testing
环境中的PHPUnit测试中。通过常规请求调用方法时不会发生此问题。
答案 0 :(得分:0)
问题解决了。似乎线索在我的laravel.log
:
#1 /home/vagrant/.composer/vendor/illuminate/support/helpers.php(404): explode('.', Array)
我想,因为我的Homestead盒子已经安装了一段时间之前,该目录中的依赖关系从未更新过,所以composer update
内部的/home/vagrant/.composer
快速完成了这一操作。