-2

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>
Karl Hill
  • 9,745
  • 4
  • 52
  • 77
  • 1
    Similar question with additional answers: [How to use dd() without stopping the program on laravel](https://stackoverflow.com/questions/68287207/how-to-use-dd-without-stopping-the-program-on-laravel) – Moshe Katz Apr 04 '22 at 18:49

1 Answers1

5

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().

Moshe Katz
  • 14,409
  • 7
  • 63
  • 108
  • I want to print this data on the index.blade.php page, but when I use it this way, one data comes. Why is that ? Message:{{$conversationDesc}} – ibrahimozden Apr 04 '22 at 19:21
  • I understand but I see this data in index.blade. return view('admin.c2cmessages.index', compact('c2cmessages', 'conversationDesc')); It can be used there as {{$conversationDesc}} . Again, only one data is coming. – ibrahimozden Apr 04 '22 at 19:24
  • @ibrahimozden That's not the same as the question you originally asked. If you [edit](https://stackoverflow.com/posts/71742295/edit) the question to update the code to show what you are actually trying to do (or ask a new question including this detail), I will see if I can answer it. – Moshe Katz Apr 04 '22 at 20:21