dimanche 15 avril 2018

Filter out Records Based on two Keys of Interchangeable Order

I am creating a private messaging system for part of a client project.

I am storing each message as a record with the values for from as the ID of the sender, to as the ID of the receiver, and message as the message text.

I would like to have a page in which users can view a list of other users that they have a messaging thread with. I am retrieving all relevant messages, that the user is a sender or receiver of:

$thisProfile = Auth::user()->profile_id;

$messages = DB::table('messages')
    ->where('from', '=', $thisProfile)
    ->orWhere('to', '=', $thisProfile)
    ->get();

return view('messages.all', compact('messages'));

However, that is obviously returning records where the same two users are mentioned. Below are some example records that I am getting when logged in as user 1:

#1 - from: 1, to: 2
#2 - from: 2, to: 1
#3 - from: 1, to: 2
#4 - from: 4, to: 1
#5 - from: 1, to: 4
#6 - from: 9, to: 1

I would like to filter out the records where the same two users have been retrieved before. In this case, the results should look like:

#1 - from: 1, to: 2
#4 - from: 4, to: 1
#6 - from: 9, to: 1

The closest I have got is finding the unique() method used with collections, which filters out records for either to and from in one order.

How can I filter out records, based on the two keys to and from, where they are of interchangeable order?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire