26

After I write:

Route::get('/', function()
{
    dd(User::all());
});

And after I refresh the browser I get an unreadable array. Is there a way to get that array in a readable format?

Gilko
  • 2,280
  • 8
  • 36
  • 45

9 Answers9

42

dd() dumps the variable and ends the execution of the script (1), so surrounding it with <pre> tags will leave it broken. Just use good ol' var_dump() (or print_r() if you know it's an array)

Route::get('/', function()
{
    echo '<pre>';
    var_dump(User::all());
    echo '</pre>';
    //exit;  <--if you want
});

Update:

I think you could format down what's shown by having Laravel convert the model object to array:

Route::get('/', function()
{
    echo '<pre>';
    $user = User::where('person_id', '=', 1);
    var_dump($user->toArray()); // <---- or toJson()
    echo '</pre>';
    //exit;  <--if you want
});

(1) For the record, this is the implementation of dd():

function dd()
{
    array_map(function($x) { var_dump($x); }, func_get_args()); die;
}
Damien Pirsy
  • 25,319
  • 8
  • 70
  • 77
  • 1
    This gives indeed a readable array :) Another problem is that it gives a very large array and the information I need is at the bottom of the page. – Gilko Dec 25 '13 at 09:47
  • Well, what information do you want? You could dump other methods to filter down to what you actually need. User::all() returns the whole object indeed – Damien Pirsy Dec 25 '13 at 09:49
  • I just want the array of the User object. I gave this in my route: User::find(6)->where('person_id' == 1) and the page gives me an array that begins with "Illuminate\Database\Eloquent\Builder Object (..." I have to scroll al the way down to get the array of the User object. – Gilko Dec 25 '13 at 09:56
  • Try having Lravel convert to array, see updated answer – Damien Pirsy Dec 25 '13 at 10:02
  • I think the where() syntax was inaccurate. I almost never use Eloquent, I have to admit (always prefer the Query Builder)) – Damien Pirsy Dec 25 '13 at 10:06
  • You could be right, because when I give var_dump(User::all()); it only gives me the User object and not the additional information. – Gilko Dec 25 '13 at 10:10
  • use dump as it wont exit the code after printing – Makamu Evans Sep 08 '21 at 20:11
30

actually a much easier way to get a readable array of what you (probably) want to see, is instead of using

dd($users); 

or

dd(User::all());

use this

dd($users->toArray());

or

 dd(User::all()->toArray());

which is a lot nicer to debug with.

EDIT - additional, this also works nicely in your views / templates so if you pass the get all users to your template, you can then dump it into your blade template

{{ dd($users->toArray()) }}
John Smith
  • 1,812
  • 1
  • 16
  • 17
3

Maybe try kint: composer require raveren/kint "dev-master" More information: Why is my debug data unformatted?

Community
  • 1
  • 1
chesscov77
  • 770
  • 8
  • 14
2

For everyone still searching for a nice way to achieve this, the recommended way is the dump() function from symfony/var-dumper.

It is added to documentation since version 5.2: https://laravel.com/docs/5.2/helpers#method-dd

Angelin Calu
  • 1,905
  • 8
  • 24
  • 44
1

You can use this code on view.blade. {{var_dump($animales)}}

JairoX
  • 41
  • 3
1

Weird that nobody pointed out to Symfony VarDumper, simply do:

\Symfony\Component\VarDumper\VarDumper::dump($data);
shamaseen
  • 2,193
  • 2
  • 21
  • 35
0

You can use var_dump or print_r functions on Blade themplate via Controller functions :

class myController{

   public function showView(){
     return view('myView',["myController"=>$this]);
   }
   public function myprint($obj){
     echo "<pre>";
     print_r($obj);
     echo "</pre>";
   }
}

And use your blade themplate :

$myController->myprint($users);
Ferhat KOÇER
  • 3,890
  • 1
  • 26
  • 26
0

I have added a helper da() to Laravel which in fact works as an alias for dd($object->toArray())

Here is the Gist: https://gist.github.com/TommyZG/0505eb331f240a6324b0527bc588769c

TommyZG
  • 500
  • 3
  • 13
0

as suggested, you can use 'die and dump' such as

dd($var)

or only 'dump', without dying,

dump($var)

novecentonove
  • 359
  • 5
  • 14