I'm not 100% sure about it but you can try.
public function sameConversationAs($id)
{
$pivotTable = DB::table("conversation_user")->where("user_id", $id)->get();
$user_conversations = array();
foreach($pivotTable as $c)
{
array_push($user_conversations, $c->conversation_id);
}
foreach($this->conversations as $c2)
{
if(in_array($c2->conversation_id, $user_conversations))
{
return true;
}
}
return false;
}
Then you can use:
$user = User::find(1);
if($user->sameConversationAs(2))
{
// User have same conversation as user with id 2
}
I believe I came up with similar solution like yours:
$exists = false;
foreach (Auth::user()->conversations as $key => $value1) {
foreach ($user->conversations as $key => $value2) {
if($value1->id == $value2->id) $exists = true;
}
}
if($exists){
foreach (Auth::user()->conversations as $key => $value1) {
foreach ($user->conversations as $key => $value2) {
if($value1->id == $value2->id){
$conversation = Conversation::find($value1->id);
}
}
}
}else{
$conversation = new Conversation;
$conversation->save();
$conversation->user()->attach([Auth::user()->id, $user->id]);
}
Thank you for your help.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community