0

This is the code I am running:

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

This is the stack trace of errors:

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

I am just trying to print the value from the database. I know that I am trying to print an object, but I want to treat it as a string. The question is, how do I get the data as a string, such that I can echo it. Thanks for all the help!

Jack

UPDATE: When I can var_dump, I get:

object(stdClass)#176 (2) { ["passageName"]=> string(10) "Developers" ["content"]=> string(26) "Jack, Gen" } 
7
  • $randomPassage is an array you can't put it in session. What you want to put in session? Commented Apr 24, 2017 at 18:38
  • I want to get a random passage from my database, then print that random passage. Commented Apr 24, 2017 at 18:39
  • can you plz tell me the db column name of table passages which want to store in session? Commented Apr 24, 2017 at 18:40
  • The table name is passages. The column that I want is content, but there is another column with passageTitle. Commented Apr 24, 2017 at 18:44
  • Just store it as json_encode() and then json_decode() it in your view for an easy work around. Commented Apr 24, 2017 at 18:54

1 Answer 1

2

to echo just one of the object parts:

echo $randomPassage->content;
Sign up to request clarification or add additional context in comments.

3 Comments

But, how do I echo just one of the parts of the object?
if you want to echo just one of the parts don't use json_encode, try to do this: echo $randomPassage->content;
That worked!!! Change you answer to "echo($randomPassage->content);", and I will mark it correct. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.