我正在尝试在我的网站上输出所有零售商的价格,但是当我在查询结束时使用 - > get()时,它会向我显示此错误。
htmlentities()期望参数1为字符串,给定对象
代码:
$price = \DB::table('cpus')->where('slug' ,'=', $slug)->join('prices', 'cpus.model', '=', 'prices.model')->join('retailers', 'retailers.id', '=', 'prices.retailer_id')->select('prices.price', 'prices.url')->get();
但是当我使用 - > first()而不是 - > get()查询是成功的,一切都在我的网站上正确显示。
$price = \DB::table('cpus')->where('slug' ,'=', $slug)->join('prices', 'cpus.model', '=', 'prices.model')->join('retailers', 'retailers.id', '=', 'prices.retailer_id')->select('prices.price', 'prices.url')->first();
我如何显示所有记录而不是一个,以及使用 - > get()会出现什么问题?
控制器:
$price = \DB::table('cpus')->where('slug' ,'=', $slug)->join('prices', 'cpus.model', '=', 'prices.model')->join('retailers', 'retailers.id', '=', 'prices.retailer_id')->select('prices.price', 'prices.url')->first();
$data = [
'price' => $price
];
return view('part', $data);
查看
@foreach ($price as $p => $value)
<h5> {{$p}}</h5> <br>
<p>{{$value}}</p>
@endforeach
完整错误
in helpers.php line 531
at CompilerEngine->handleViewException(object(ErrorException), '1') in PhpEngine.php line 44
at PhpEngine->evaluatePath('C:\xampp\htdocs\laravel\storage\framework\views/c6f0b1264595e5a7c0dccbaeda0b9c53ec2bb0a2.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'title' => 'Intel Core i3-4170 - BX80646I34170', 'part_name' => 'Intel Core i3-4170 3.7GHz 2-kodolu procesors', 'specs' => object(stdClass), 'img' => array(object(stdClass), object(stdClass)), 'price' => array(object(stdClass), object(stdClass)))) in CompilerEngine.php line 59
at CompilerEngine->get('C:\xampp\htdocs\laravel\resources\views/part.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'title' => 'Intel Core i3-4170 - BX80646I34170', 'part_name' => 'Intel Core i3-4170 3.7GHz 2-kodolu procesors', 'specs' => object(stdClass), 'img' => array(object(stdClass), object(stdClass)), 'price' => array(object(stdClass), object(stdClass)))) in View.php line 149
at View->getContents() in View.php line 120
at View->renderContents() in View.php line 85
at View->render() in Response.php line 53
at Response->setContent(object(View)) in Response.php line 199
at Response->__construct(object(View)) in Router.php line 1087
at Router->prepareResponse(object(Request), object(View)) in ControllerDispatcher.php line 94
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96
at ControllerDispatcher->callWithinStack(object(cpuController), object(Route), object(Request), 'showCpu') in ControllerDispatcher.php line 54
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\cpuController', 'showCpu') in Route.php line 174
at Route->runController(object(Request)) in Route.php line 140
at Route->run(object(Request)) in Router.php line 724
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 64
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 62
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Router.php line 726
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 699
at Router->dispatchToRoute(object(Request)) in Router.php line 675
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 53
答案 0 :(得分:2)
基本上,foreach中的两个值中的一个或多或少是数组等而不是字符串。
通过在foreach之后添加{{dd()}}
来调试它,以获取其输出值以查看结果。有点像...
@foreach ($price as $p => $value)
{{dd($p)}}
<h5> {{$p}}</h5> <br>
<p>{{$value}}</p>
@endforeach
如果$p
我很好地将其翻出来然后为$value
你可能会发现这是一个数组/集合类型的结果,所以可能需要做类似的事情
$value->price
但是看看dd产生了什么等等