List of users ( I have contacted or they have contacted me first )


I’m working on a Laravel Project ( Messaging System ) And I would like to get my conversations list, People who contacted me first or I have contacted

This is my models and relationships :

Schema::create('messages', function (Blueprint $table) {




Message Model :

class Message extends Model
    protected $fillable = ['body', 'subject', 'sent_to_id', 'sender_id'];

    // A message belongs to a sender
    public function sender()
        return $this->belongsTo(User::class, 'sender_id');

    // A message also belongs to a receiver    
    public function receiver()
        return $this->belongsTo(User::class, 'sent_to_id');

User Model

class User extends Authenticatable
    use Notifiable;

    protected $fillable = ['name', 'email', 'password'];

    // A user can send a message
    public function sent()
        return $this->hasMany(Message::class, 'sender_id');

    // A user can also receive a message
    public function received()
        return $this->hasMany(Message::class, 'sent_to_id');

Now I’m displaying the users who contacted me like that


I would like to get a list of the users who have contacted me or I have contacted first ( just like facebook inbox for example )

Source: Laravel

Categorised as laravel Tagged


Leave a Reply

Still Have Questions?

Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us