Skip to content

WherePivot = is null on FK #2

@Grazouillis

Description

@Grazouillis

Hello Mr. Korop,

Hello everybody. I've been noticing a bug in my app for days and I'm unable to understand and fix it.

I try to create a social network, and I have a "connections" table in my database, which is a pivot table with extra fields.
Here is my migration file:

            // $table->id();
            $table->comment('self table users-users friendships');

            $table->unsignedInteger('requester_user_id');
            $table->unsignedInteger('recipient_user_id');
            $table->enum('status', ['pending', 'approved', 'declined'])->default('pending');
            $table->date('connected_since')->nullable();

            $table->foreign('requester_user_id')->references('id')->on('users')
                ->constrained()
                ->onUpdate('cascade')
                ->onDelete('cascade');

            $table->foreign('recipient_user_id')->references('id')->on('users')
                ->constrained()
                ->onUpdate('cascade')
                ->onDelete('cascade');

            $table->timestamps();
        });

DB-table capture : https://user-images.githubusercontent.com/8795284/211657973-b2fd4325-52da-44aa-80cd-c564feb68b00.png

My Model:

public function connections()
    // relation M-M
    {
        $query =  $this->belongsToMany(
            self::class,
            'connections',
            'requester_user_id',
            'recipient_user_id'
        )->withTimestamps()
        ->withPivot('status')
        ->withPivot('connected_since');

dd($query->toSql());

return $query;
}

When my dd() is executed, I have this sql query:
"select * from users inner join connections on users.id = connections.recipient_user_id where connections.requester_user_id is null" // app/Models/User.php:73
I don't understand why I have a "connections.requester_user_id is null" because I didn't emit this info in my code.
With this condition I always have an empty collection/array as result

complete dd()

I tried to export my model from User to Connection

User.php:

$query = $this->belongsToMany(
            User::class,
            'connections',
            'requester_user_id',
            'recipient_user_id'
        )->withTimestamps()
            ->using(Connection::class);

Connection.php:

{
    use HasFactory;

    protected $primaryKey = null;

 public function connections()
    {
        return $this->belongsToMany(
            User::class,
            'connections',
            'recipient_user_id',
            'requester_user_id'
        )->withTimestamps()
            ->withPivot('status', 'connected_since'); // nécessaire : permet de trouver les valeurs externes de la table M-M hors FK
    }

    public function sendConnectionInvitation()
    {
        return $this->connections()
            ->withPivotValue('requester_user_id', auth()->id());
    }

    // connexions reçues
    public function approvedConnections()
    {        
        return $this->belongsToMany(
            User::class,
            'connections',
            'requester_user_id',
            'recipient_user_id'
            )->withTimestamps()
            ->withPivot('status', 'connected_since')
            ->wherePivot('status', '=', 'approved');
}
[...]
}

That mention : protected $primaryKey = null; doesn't solve my problem... it still doesn't work... wherePivotNotNull either
I also tried to create a primary ID next to the 2 FK in case of via my migrations, but the problem still the same.

Lastly, I tried your method,

public function following()
    {
        return $this->belongsToMany(User::class, 'connections', 'requester_user_id', 'recipient_user_id');
    }

    public function followed()
    {
        return $this->belongsToMany(User::class, 'connections', 'recipient_user_id', 'requester_user_id');
    }

// Connections

    public function followingRequest()
    {
        return $this->following()
            ->withPivotValue('requester_user_id', auth()->id())
            ->withTimestamps();
    }

public function approvedConnections() {

    $q = $this->following()
 //   ->wherePivot('status', '=', 'approved')
    ->where('status', '=', 'approved')
    ->toSql();

    dd($q);
}

I was hopeful with this way to help me...
but the result still the same : "select * from usersinner joinconnectionsonusers.id=connections.requester_user_idwhereconnections.recipient_user_id**is null** andconnections.status = ? ◀" // app/Models/User.php:99

I found a few questions which have the same result than mine, but nothing fixes my problem.
I guess you won't have the time to read such a pamphlet, but when in doubt, I leave it to you, Mr. Laravel
Thank you at least for reading me!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions