以前的版本里,当我们要打印输出一个collection里的数据时,往往是先给这个collection取个变量,然后再丢到dd()
或dump()
中。现在,5.5中,我们直接可以在collection上来调用这两个方法。
$posts = Post::all();
$posts
->dump()
->sorBy('title')
->dump()
->pluck('title')
->dump();
看到没,我们直接在每一步后面加上dump()
方法,那么输出是怎样的呢?
Collection {#284 ▼
#items: array:3 [▼
0 => Post {#285 }
1 => Post {#286 }
2 => Post {#287 }
]
}
Collection {#272 ▼
#items: array:3 [▼
0 => Post {#285 }
2 => Post {#287 }
1 => Post {#286 }
]
}
Collection {#268 ▼
#items: array:3 [▼
0 => "Aida Bosco"
1 => "Madge Leuschke"
2 => "Miss Bulah Armstrong Jr."
]
}
也即每遇到一个dump()
就输出一下数据,对于测试每一步的数据非常方便。当然这里dump()
和dd()
有所差异,dump()
是输出当前的执行结果,并且程序继续运行,而dd()
则代表dump and die
,也即输出当前的数据,然后结束运行。
$posts = Post::all();
$posts
->dump()
->sorBy('title')
->dd()
->pluck('title')
->dump();
这个时候就肯定只有两个结果输出了:
Collection {#284 ▼
#items: array:3 [▼
0 => Post {#285 }
1 => Post {#286 }
2 => Post {#287 }
]
}
array:3 [▼
0 => Post {#285 }
2 => Post {#287 }
1 => Post {#286 }
]