There are 2 data in MyController.php
public function index(Request $request, $conversation_id)
{
}
These two data are returning "1abcde" and "abcdefgh."
index.blade.php
There are 2 data in MyController.php
public function index(Request $request, $conversation_id)
{
}
These two data are returning "1abcde" and "abcdefgh."
index.blade.php
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()
.