hulapoua nusaka hulapoua nusaka - 6 months ago 685
MySQL Question

laravel 5.2 queries - htmlentities() expects parameter 1 to be string, object given

I am trying to output all retailer prices on my website , but when i am using ->get() at the end of the query it shows me this error.


htmlentities() expects parameter 1 to be string, object given


Code:

$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();


But when i am using ->first() instead of ->get() query is sucessful and everything shows properly on my website.

$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();


How can i show all records instead of one, and what would be the problem with using ->get()?

Controller :

$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);


View

@foreach ($price as $p => $value)
<h5> {{$p}}</h5> <br>
<p>{{$value}}</p>
@endforeach


Full error

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

Answer

basically one of both of the values with in the foreach is more or less an array etc and not a string.

debug it by adding {{dd()}}just after the foreach to get the value its outputting to see what the result is. Something like...

 @foreach ($price as $p => $value)

      {{dd($p)}}    

    <h5> {{$p}}</h5> <br>
    <p>{{$value}}</p>
 @endforeach

if the $pi fine swop it out then for the $valueyou might find that this is an array/collection type of result so might need to do something like

   $value->price

But see what the dd produce's etc