vendredi 3 mai 2019

how to get record of immageable type by id in laravel morph relationship

we have developed the message system in which we established two morph relationships with 4 table, staff send message to student and student send messages to staff I want to get the received messages from the relationship I established get what if I want to get the received message of a certain model with an id

for example, if student "X" send message to staff "A"

and student "Y" send message to staff A when I query staff A's received messages I got the two messages but when I want to get the message of student X

how we get it?

tbl_message

id messageable_id, messageable_type, subject, message

1     1              app\staff        msg1     msg1

2     2              app\staff        msg1     msg1

3     1               app\student    msg2        msg2

4     2               app\student    msg2        msg2

tbl_message_receiver

id, message_id , receiveable_id, receiveable_type

1     1            1                 app\student

2    2            2                 app\student

3    3            1                 app\staff

3    4            1                 app\staff

staff

id name
1   A
2   B

student

id name
1   X
2   Y

class Message extends Model
{

    public $table = "tbl_message";

    public function messageable()
    {
        return $this->morphTo();
    }
}

class Messagereceive extends Model
{
    public $table = "tbl_message_receiver";
    public function receiveable()
    {
        return $this->morphTo();
    }

    public function message()
    {
         return $this->hasOne('App\Message');
    }
}

class Staff extends Model
{

    public function messages()
    {
        return $this->morphMany('App\Message', 'messageable');
    }

    public function receives()
    {
        return $this->morphMany('App\Messagereceive', 'receiveable');
    }
}

class Student extends Model
{

    public function messages()
    {
        return $this->morphMany('App\Message', 'messageable');
    }


    public function receives()
    {
        return $this->morphMany('App\Messagereceive', 'receiveable');
    }
}

tbl_message
id messageable_id, messageable_type, subject, message

3     1               app\student    msg2        msg2
4     2               app\student    msg2        msg2
`````
`````
tbl_message_receiver
id, message_id , receiveable_id, receiveable_type
1     1            1                 app\student
2    2            2                 app\student
3    3            1                 app\staff
4    4            1                 app\staff

`````

`````
$staff = \App\Staff::find(1);
$all_message_recieves = $staff->receives;
````

````
3    3            1                 app\staff
4    4            1                 app\staff

````

**I want to get receive messages from student id 1**
and how to get message subject and message content 




from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2VEZFrh
via IFTTT

Aucun commentaire:

Enregistrer un commentaire