You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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_idis 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
{
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!
The text was updated successfully, but these errors were encountered:
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:
DB-table capture : https://user-images.githubusercontent.com/8795284/211657973-b2fd4325-52da-44aa-80cd-c564feb68b00.png
My Model:
When my dd() is executed, I have this sql query:
"select * from
users
inner joinconnections
onusers
.id
=connections
.recipient_user_id
whereconnections
.requester_user_id
is null" // app/Models/User.php:73I 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:
Connection.php:
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,
I was hopeful with this way to help me...
but the result still the same :
"select * from
usersinner join
connectionson
users.
id=
connections.
requester_user_idwhere
connections.
recipient_user_id**is null** and
connections.
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!
The text was updated successfully, but these errors were encountered: