In Laravel, through $text = DB::table('articles')->select('text')->get(); got a variable with the result `Object (

  [items:protected] => Array ( [0] => stdClass Object ( [text] => Vestibulum. ) ) )` 

And the text is displayed in this way

[{"text": "Vestibulum."}]

So the question is how to access the object to get the text? And one more optional question, why does Laravel cost objects with three-story arrays in order to convey just the text?

  • one
    $text = DB::table('articles')->select('text')->get()->toArray(); - robertobadjio
  • Issued an array with the object, it is also not clear how to get to the text. 'Array ([0] => stdClass Object ([text] => Vestibulum.))' @Robertobadjo - Swift
  • one
    $text[0] -> text; - The fast
  • "Learn all" about the object, method, function in PHP will help Reflection API. In your case this is ReflectionClass . Discover Reflection, and stop searching by code. - Total Pusher

2 answers 2

And one more optional question, why does Laravel cost objects with three-story arrays in order to convey just the text?

What you ask of him, he builds, read the documentation.

If you want to get only a value, use the value method if you need one value:

 $text = DB::table('articles')->value('text'); // строка 

Or if you need an array of all values, use pluck :

 $text = DB::table('articles')->pluck('text')->toArray(); // массив строк. 

    I do not advise writing on laravel DB :: ... best to write Eloquent-oh-the standard laravel, but if so get () returns an object and you should do this in view so here is an example

      @foreach ($db as $item) {{$item['name']}} //елементи DB {{$item['email']}} @endforeach {{}}->обратиться к объекту 
    • Why is DB so bad? - Yaroslav Molchan
    • no, it's not bad, just laravel has standards with them; the code makes it faster to write better Eloquent- oh