Array to string conversion (View) Hi Guys, I've a quick question somehow i'm getting this error
Array to string conversion (View)
here is my logic
@foreach ($users as $user)
{!! $user !!}
@endforeach
if I do dd() it is working. without dd() getting above error
Thanks.
Is $users and array/collection of User's model? Each item from that array will be an object and when blade tries to output it as a string it will fail.
Try outputting the user's attributes:
@foreach ($users as $user)
{{ $user->id }} / {{ $user->email }}
@endforeach
If you need, for any reason, you need to show the user object you can try the @json (...) blade directive
@foreach ($users as $user)
@json($user)
@endforeach
@rodrigo.pedra $users is an array. if
dd($user) getting like this
@foreach ($crud as $user)
{{ dd($user) }}
@endforeach
output:
array:8 [▼
"id" => 766
"create_time" => "2019-10-18T17:51:17Z"
"update_time" => "2019-11-18T17:51:37Z"
"state" => "error"
"error" => "no record"
"name" => "john"
]
You probably need a nested foreach. Hard to teel what you are doing.
Like this example:
public function testJson()
{
$data = '[{
"ClientID":24,
"Name":"Client1",
"Balance1":null,
"Balance2":null
},
{
"ClientID":25,
"Name":"Client2",
"Balance1":24,
"Balance2":0
}]';
//dd($data);
$decoded = json_decode($data, true);
//dd($decoded);
foreach ($decoded as $d) {
foreach ($d as $k => $v) {
echo "$k - $v\n";
}
}
}
But use some trial and error.
@deepu07
You are trying to echo out an array as a string. You just need to specify a key:
@foreach ($users as $user)
{{ $user['name'] }}
@endforeach
Also be careful with using {!! !!}
@talinon beat me to it. I think this is the correct answer.
Please sign in or create an account to participate in this conversation.