Reputation: 644
I recieve correctly this two messages , but I need to JOIN this two results with Laravel5.
Could anyone helps to me ? Or it's any possibilities to doing this with other form?
$mensajes = Messageuser::find(1)->conversaciones()->get();
$mensajes2 = Messageuser::find(4)->conversaciones()->get();
echo $mensajes;
echo $mensajes2;
UPDATE
The MessageUser
use Illuminate\Database\Eloquent\Model;
class MessageUser extends Model {
protected $table ="message_user";
public function conversaciones(){
return $this->hasMany('App\Message','id_message','id');
}
}
The Message model
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Message extends Model {
protected $table="messages";
public function chat()
{
return $this->belongsTo('App\MessageUser','id');
}
}
UPDATED2
When I tried to sort in the view appear Illegal offset type
$mensajes = Messageuser::find(1)->conversaciones()->get();
$mensajes2 = Messageuser::find(4)->conversaciones()->get();
$mensajes = $mensajes->keyBy('created_at');
$prueba = $mensajes->merge($mensajes2->keyBy('created_at'));
return view('admin.msj')->with('pruebas',$prueba);
Upvotes: 0
Views: 99
Reputation: 6730
The relation is a hasMany
and will therefor return a collection set, not one "echoable" thing:
$mensajes = Messageuser::find(1)->conversaciones()->get();
$mensajes2 = Messageuser::find(4)->conversaciones()->get();
\dd($mensajes, $mensajes2);
This will return two Collection
objects dumped. Please also note that you can use:
$mensajes = Messageuser::find(1)->conversaciones;
To get the same return set.
Now you want to join the results, you could use the built-in method for merge
from the Collection
class:
$mensajes->merge($mensajes2);
To merge based on created_at:
$mensajes = $mensajes->keyBy('created_at');
$mensajes->merge($mensajes2->keyBy('created_at'));
Upvotes: 2