-2

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

amanigurmu
  • 11
  • 6
  • 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
  • 15,992
  • 7
  • 69
  • 116
  • @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