Laravel modify Collection data
<p>I wonder if Laravel have any helper to modify a collection.</p>
<p>What I need to do is to make a query with <code>paginate()</code> then check if the logged in users ID match the sender or receiver and based on that add a new value to the output:</p>
<pre><code>$userId = Auth::guard('api')-&gt;user()-&gt;user_id;
$allMessages = Conversation::join('users as sender', 'conversations.sender_id', '=', 'sender.user_id')
                               -&gt;join('users as reciver', 'conversations.recipient_id', '=', 'reciver.user_id')
                               -&gt;where('sender_id',$userId)-&gt;orWhere('recipient_id',$userId)
                               -&gt;orderBy('last_updated', 'desc')
                               -&gt;select('subject','sender_id','recipient_id', 'sender_unread', 'recipient_unread', 'last_updated', 'reciver.username as receivername', 'sender.username as sendername')
                               -&gt;paginate(20);
</code></pre>
<p>Now I want to do something like:</p>
<pre><code>if ($allMessages-&gt;sender_id == $userId) {
    // add new value to output
    newField = $allMessages-&gt;sendername
} else {
    // add new value to output
    newField = $allMessages-&gt;receivername
}
</code></pre>
<p>Then send the data with the new value added</p>
<pre><code>return response()-&gt;json(['messages' =&gt; $allMessages], 200);
</code></pre>
<p>Is this possible?</p>