Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

amarsyla's avatar

Eloquent Reverse Relations Query

Hey guys, I have two eloquent models:

User:

<?php namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

    use Authenticatable, CanResetPassword;

    protected $dates = ['birthday'];

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'email', 'password'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

    public function connections()
    {

        return $this->belongsToMany('App\Connection');
    }

    public function addConnection(Connection $connection)
    {
        $this->connections()->attach($connection->id);
    }

    public function removeConnection(Connection $connection)
    {
        $this->connections()->detach($connection->id);
    }

    public static function boot()
    {
        parent::boot();

        User::deleting(function($user)
        {
            $user->connections()->detach();
        });
    }

}

Connection:

<?php namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class Connection extends Model implements AuthenticatableContract, CanResetPasswordContract {

    use Authenticatable, CanResetPassword;

    protected $dates = ['birthday'];

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'connections';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'email', 'image', 'gender', 'phone', 'company', 'function', 'social', 'address', 'city', 'postcode', 'birthday'];

    public function post()
    {
        return $this->belongsTo('App\User');
    }
}

I have three tables, a table for users, one for connections and another one named connection_user, which has columns id, user_id and connection_id.

With the code below, I can successfully extract all connections related to a user:

$connections = $user->connections()->simplePaginate(6);

What if I wanted to do its reverse, getting all the users related to a connection? Is that possible at the current state?

Thanks.

0 likes
2 replies
rodrigo.pedra's avatar
Level 56

Change the users relation on the Connection model to also use the belongsToMany relationship, and as you are using a different name for your method, pass the other arguments too, like so:

public function post()
{
    return $this->belongsToMany('App\User', 'connection_user', 'user_id', 'connection_id');
}

Then you could access the related users pretty much the same way you are doing with users: $connection->post()->simplePaginate();

See this laracast: [ https://laracasts.com/series/laravel-5-fundamentals/episodes/21 ]. Actually the whole series is worth watching.

code_chris's avatar

Not sure why your relationship method is called post(), that threw me off a bit.

Not sure how your connections are meant to work but in my mind I imagine a connection will only belong to one user?

Please or to participate in this conversation.