Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I'm trying to make an array on my contact list, basing on my message table
fromContact
relation:
$messages = Message::where('offer_id', $id)->get();
$contacts = array();
foreach($messages as $message){
$contacts[] = $message->fromContact;
next I'm trying to make a map on contact, using $unreadIds that are result of other query on messages table:
$contacts = $contacts->map(function($contact) use ($unreadIds) {
$contactUnread = $unreadIds->where('sender_id', $contact->id)->first();
$contact->unread = $contactUnread ? $contactUnread->messages_count : 0;
return $contact;
and this is not working... I've got simply message with error:
Call to a member function map() on array
and I understand it, I should not use map() on array - so I tried many ways to convert it to object - all failed.
for example
converting contacts into object after array loop
$contacts = (object)$contacts;
gives error: "message": "Call to undefined method stdClass::map()",
Maybe anyone knows how to fix that?
$collection->map(function($contact) use ($unreadIds) {
$contactUnread = $unreadIds->where('sender_id', $contact->id)->first();
$contact->unread = $contactUnread ? $contactUnread->messages_count : 0;
return $contact;
https://laravel.com/docs/5.6/collections
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.