jeudi 26 septembre 2019

Create indirect relationships and return the sum of two relationships

I have a program to share services between people. Basically I am facing a problem trying to get the conversations between users. Here is the idea: My program allows users to offer services to other users, but also to acquire services from other users. Each user can register an unlimited number of services. When a user finds a service that he/she wants to get, he creates a deal and starts a conversation with the counterpart. The structure of my tables is the following (I am only including the relationship columns):

Users table

id // This is the user identifier

Services table

id // This is the service identifier
user_id // This is the identifier of the user who created the service

Deals table

id // This is the deal identifier
user_id // This is the identifier of the user acquiring the service
service_id // This is the identifier of the service being acquired in this deal

Conversations table

id // This is the identifier of the conversation
deal_id // This is the identifier of the deal that the conversation belongs to

Here is my problem: I want to be able to retrieve the conversations for each user, both as applicant and as a seller.

I created this relationship (in User.php) for conversations in which the user is acquiring the service:

    public function conversationsApplicant(){
        return $this->hasManyThrough( Conversations::class, Deal::class );
    }

I would like to create also the conversationsSeller() function

    public function conversationsSeller(){
        return $this->????;
    }

I am guessing I should add some kind of relationship between a Conversation and a Service in Service.php. It would be something like $this->services()->with( 'deals' )->with( 'conversations' );

The final goal would be to have a method that returns both relationships in one $user->conversations()->get().

public function conversations(){
    return $this->conversationsApplicant() + $this->conversationsSeller();
}


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire