Laravel Error - 类stdClass的对象无法转换为字符串

时间:2017-04-24 18:35:16

标签: php mysql laravel laravel-5 laravel-4

这是我正在运行的代码:

  //FIXME
  $data = $request->all();
  $randomPassage = DB::table('passages')->inRandomOrder()->first();
  session()->put('passage', $randomPassage);
  echo($randomPassage);

这是错误的堆栈跟踪:

in routes.php line 31
at HandleExceptions->handleError('4096', 'Object of class stdClass could not be converted to string', 'C:\xampp\htdocs\spring2017-cp-443965-441701\dev-develop\app\Http\routes.php', '31', array('request' => object(Request), 'data' => array('_token' => 'POr6dhVLdDza0O0jHLbWy9daKr3nkyA94EIm4wHM'), 'randomPassage' => object(stdClass))) in routes.php line 31
at RouteServiceProvider->{closure}(object(Request))
at call_user_func_array(object(Closure), array(object(Request))) in compiled.php line 8519
at Route->runCallable(object(Request)) in compiled.php line 8509
at Route->run(object(Request)) in compiled.php line 8225
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 compiled.php line 3225
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in compiled.php line 9963
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 compiled.php line 13474
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in compiled.php line 9963
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 compiled.php line 11964
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in compiled.php line 9963
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 compiled.php line 13213
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in compiled.php line 9963
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 compiled.php line 13150
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in compiled.php line 9963
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 compiled.php line 9948
at Pipeline->then(object(Closure)) in compiled.php line 8226
at Router->runRouteWithinStack(object(Route), object(Request)) in compiled.php line 8217
at Router->dispatchToRoute(object(Request)) in compiled.php line 8207
at Router->dispatch(object(Request)) in compiled.php line 2419
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 compiled.php line 3286
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in compiled.php line 9963
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 compiled.php line 9948
at Pipeline->then(object(Closure)) in compiled.php line 2366
at Kernel->sendRequestThroughRouter(object(Request)) in compiled.php line 2350
at Kernel->handle(object(Request)) in index.php line 54
at require_once('C:\xampp\htdocs\spring2017-cp-443965-441701\dev-develop\public\index.php') in server.php line 21

我只是想从数据库中打印值。我知道我正在尝试打印一个对象,但我想将它视为一个字符串。问题是,如何将数据作为字符串获取,以便我可以回应它。谢谢你的帮助!

杰克

更新:当我可以var_dump时,我得到:

object(stdClass)#176 (2) { ["passageName"]=> string(10) "Developers" ["content"]=> string(26) "Jack, Gen" } 

1 个答案:

答案 0 :(得分:2)

只回显一个对象部分:

echo $randomPassage->content;