Laravelで変数のデータを確認するのにdd($val);という関数が用意されていますが、これだと情報が多すぎてDBの中身の配列だけを確認するには使いにくいです。下記のような使い方が実用的で便利です。
View(xxxx.blade.php)で変数を出力する場合は、下記のようなプログラムが便利です。
<pre> @php var_dump($post->toArray() ); @endphp </pre>
<pre>タグで挟んでおくと配列が改行されて見易くなります。
$postだけだと情報が多すぎるので$post->toArray() とします。
これによって最低限必要なデータをシンプルに表示することができます。
post(親)とtag(子)のテーブルのデータの場合、下記のように出力されます。
array(6) {
["id"]=>
int(10)
["name"]=>
NULL
["body"]=>
string(33) "CentOSにPHPをインストール"
["created_at"]=>
string(27) "2020-04-18T14:34:14.000000Z"
["updated_at"]=>
string(27) "2020-04-18T14:34:14.000000Z"
["tags"]=>
array(2) {
[0]=>
array(5) {
["id"]=>
int(3)
["name"]=>
string(3) "PHP"
["created_at"]=>
string(27) "2020-04-18T00:30:45.000000Z"
["updated_at"]=>
string(27) "2020-04-18T00:30:45.000000Z"
["pivot"]=>
array(2) {
["post_id"]=>
int(10)
["tag_id"]=>
int(3)
}
}
[1]=>
array(5) {
["id"]=>
int(5)
["name"]=>
string(5) "Linux"
["created_at"]=>
string(27) "2020-04-18T00:31:06.000000Z"
["updated_at"]=>
string(27) "2020-04-18T00:31:06.000000Z"
["pivot"]=>
array(2) {
["post_id"]=>
int(10)
["tag_id"]=>
int(5)
}
}
}
}