Support the ongoing development of Laravel.io →
Database Eloquent Validation
Last updated 2 years ago.
0

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
}
Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

alenn-m alenn-m Joined 5 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.