Joining two tables, return results from one as an array property of the main table?
I have two tables, a table of messages and a table of message recipients.
I would like to get all of the messages, with the recipients for each message as a property of the message.
So the results should look something like this
0 =>
object(stdClass)[899]
public 'id' => int 1
public 'title' => string 'Message One' (length=11)
public 'recipient_ids' =>
array (size=3)
0 => int 1
1 => int 3
2 => int 7
1 =>
object(stdClass)[904]
public 'id' => int 2
public 'title' => string 'Message Two' (length=11)
public 'recipient_ids' =>
array (size=2)
0 => int 2
1 => int 8
I'm trying to use Laravel's Query Builder to accomplish this but I'm not sure if it is possible. When I try to use a join, I end up with a seperate result for each recipeint_id. This is what I'm currently trying:
$messages = DB::table('messages')
->leftJoin('message_recipients', 'messages.id', '=', 'message_recipients.message_id')
->select('messages.*', 'message_recipients.recipient_id')
->get();
Is this possible to do using Laravel's Query Builder, and if so how? I'm not finding much in the docs or on google, but I suppose its possible I'm googling the wrong terms. If this is an extremely basic question I apologize, I'm a bit of a noob when it comes to mysql.
Thanks!
Please or to participate in this conversation.