'There are 2 data in the conversationDesc section, but only one data is returned in the blade section, why?
There are 2 data in the conversationDesc section, but only one data is returned in the blade section; why? Here the same data is returned twice; why? Message:"abcdefgh" is coming twice but I want to see Message:"1abcde" and Message:"abcdefgh".
MyController.php
public function index(Request $request, $conversation_id)
{
$c2cmessages = C2CMessage::where('conversation_id', $conversation_id)->get();
foreach ($c2cmessages as $c2cmessage) {
$MessageContent = $c2cmessage["MsgBody"][0]["MsgContent"];
$decodedData = json_decode($MessageContent["Data"], true);
$conversationDesc = $decodedData["conversationDesc"];
}
return view('admin.c2cmessages.index',
compact('c2cmessages', 'conversationDesc'));
}
dump($conversationDesc);
These two data are returning "1abcde" and "abcdefgh."
index.blade.php
<ul class="contacts-block profile-box list-unstyled">
@foreach($c2cmessages as $c2cmessage)
<li class="contacts-block__contact-container">
<div class="contacts-block__contact-content">
<div class="contacts-block__contact-content__time">
<b>Message:</b>
<span class="contacts-block__contact-content__time__text">
{{$conversationDesc}}
</span>
</div>
</div>
</li>
@endforeach
</ul>
Solution 1:[1]
Inside your loop you are overwriting the value of $conversationDesc each time, so it will only ever have the last value.
Since you are passing the $c2cmessages array into the view, there is no reason to pass a separate $conversationDesc value.
The only reason you need a loop in the controller is to parse and transform the JSON object. Because the Eloquent get() function returns a Collection object, you can use the map() method like this for cleaner code:
public function index(Request $request, $conversation_id)
{
$c2cmessages = C2CMessage::where('conversation_id', $conversation_id)
->get()
->map(function ($c2cmessage) {
$messageContent = $c2cmessage["MsgBody"][0]["MsgContent"];
$decodedData = json_decode($messageContent["Data"], true);
return [
'conversationDesc' => $decodedData["conversationDesc"],
// add any other fields you need here too
];
});
return view('admin.c2cmessages.index', compact('c2cmessages'));
}
Here is what your view should look like:
<ul class="contacts-block profile-box list-unstyled">
@foreach($c2cmessages as $c2cmessage)
<li class="contacts-block__contact-container">
<div class="contacts-block__contact-content">
<div class="contacts-block__contact-content__time">
<b>Message:</b>
<span class="contacts-block__contact-content__time__text">
{{$c2cmessage['conversationDesc']}}
</span>
</div>
</div>
</li>
@endforeach
</ul>
OLD ANSWER before question was edited
dd() means dump() and die() - first it prints the value, then it kills the script execution.
If you want to see multiple values for debugging, use dump().
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
