Do you have messages_involved records with no product or order id's?
May 23, 2019
3
Level 13
laravel strange SQL query builded
I'm using the following model for a message system inside my application:
User model:
public function getMessagesInvolvedAttribute()
{
return MessageType::where('initiated_by', $this->id)->orWhere('receiver', $this->id)->with(['sender:id,slug'])->orderBy('created_at', 'desc');
}
MessageType model:
public function sender()
{
return $this->hasOne('App\User', 'id', 'initiated_by');
}
public function receiver()
{
return $this->hasOne('App\User', 'id', 'receiver');
}
public function messages()
{
return $this->hasMany('App\Message');
}
public function product()
{
return $this->hasOne('App\Product', 'id', 'product_id');
}
public function order()
{
return $this->hasOne('App\Order', 'id', 'order_id');
}
Using the following eloquent query to show some messages of the user
Auth::user()->messages_involved->with(['product', 'order'])->withCount(['messages as unread_messages' => function ($query) {
$query->where('user_id', '!=', Auth::id())->whereNull('read_on');
}])->paginate(15),
The following SQL queries are returned (using debugbar):
...some other queries...
select * from `products` where `products`.`id` in ('', 1) and `products`.`deleted_at` is null
select * from `orders` where `orders`.`id` in ('', 1, 2) and `orders`.`deleted_at` is null
I'm a bit confused since '' is included in both queries, any idea why this occurs? Are my relationships fine in general?
Please or to participate in this conversation.