php 如何显示可读数组 - Laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20771239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:32:44  来源:igfitidea点击:

How to display a readable array - Laravel

phparraysfunctionlaravelreadable

提问by Gilko

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?

刷新浏览器后,我得到一个不可读的数组。有没有办法以可读的格式获取该数组?

回答by Damien Pirsy

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)

dd()转储变量并结束脚本(1)的执行,因此用<pre>标签包围它会使其损坏。只需使用好的 ol' var_dump()(或者print_r()如果您知道它是一个数组)

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:

我认为您可以通过让 Laravel 将模型对象转换为数组来格式化显示的内容:

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():

(1) 为了记录,这是 dd() 的实现:

function dd()
{
    array_map(function($x) { var_dump($x); }, func_get_args()); die;
}

回答by John Smith

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()) }}

回答by Juan

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

也许试试kint:composer require raveren/kint "dev-master" 更多信息:为什么我的调试数据没有格式化?

回答by Angelin Calu

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

对于仍在寻找实现此目的的好方法的每个人,推荐的方法是dump()symfony/var-dumper.

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

它从 5.2 版开始添加到文档中:https: //laravel.com/docs/5.2/helpers#method-dd

回答by TommyZG

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

da()在 Laravel 中添加了一个助手,它实际上是dd($object->toArray())

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

这是要点:https: //gist.github.com/TommyZG/0505eb331f240a6324b0527bc588769c

回答by novecentonove

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

dd($var)

or only 'dump', without dying,

dump($var)

按照建议,您可以使用“die and dump”,例如

dd($var)

或仅使用“dump”,而不会死亡,

dump($var)

回答by JairoX

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

您可以在 view.blade 上使用此代码。{{var_dump($animales)}}

回答by Ferhat KO?ER

You can use var_dumpor print_rfunctions on Blade themplate via Controller functions :

您可以通过控制器功能在 Blade 模板上使用var_dumpprint_r功能:

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