Laravel关系“有时”返回null

问题描述

我对Laravel关系有一个奇怪的问题。

  • Laravel版本:7.27.0
  • 某些模型使用BinaryCabin \ LaravelUUID。

整个应用程序运行平稳,我对Laravel中的关系有一个很好的了解,但是最近我出于统计原因开始研究一些应用程序数据,并遇到了以下问题。

我在表中有签到事件:

-----------------------------------------------------------------------------------------------------------------------
|  id  | user_id                              | booking_id                           | check_in            | check_out|
-----------------------------------------------------------------------------------------------------------------------
| 1257 | b9189540-bf96-11ea-bfb2-fb5284940ca4 | bc7d2500-c779-11ea-9d49-c7bb70e58097 | 2020-07-22 10:14:18 | null     |
| 1541 | b9189540-bf96-11ea-bfb2-fb5284940ca4 | 2e8749e0-ccd5-11ea-9b46-9febde327734 | 2020-07-23 13:12:31 | null     |
-----------------------------------------------------------------------------------------------------------------------

随附的预订表已使用外键设置并且存在记录。

Schema::create('bookings',function (Blueprint $table) {
    $table->engine = 'InnoDB';
    $table->uuid('id')->primary();
    $table->uuid('user_id');
    ...
});
Schema::create('checkins',function (Blueprint $table) {
    $table->id();
    $table->uuid('user_id');
    $table->uuid('booking_id');
    $table->foreign('booking_id')->references('id')->on('bookings')->onUpdate(null)->onDelete(null);
    ...
});

我也有关系:

// Booking.php

class Booking extends Model
{
    use SoftDeletes,HasUUID;

    protected $table            = 'bookings';
    protected $uuidFieldName    = 'id';

    public function checkIn()
    {
        return $this->hasOne(CheckIn::class);
    }

    ...
// CheckIn.php

class CheckIn extends Model
{
    protected $table = 'checkins';
    
    public function booking()
    {
        return $this->belongsTo(Booking::class,'booking_id','id');
    }

    ...

我现在使用以下命令运行雄辩的查询

$user = User::where('id','b9189540-bf96-11ea-bfb2-fb5284940ca4')->first();
if ($user) 
{
    $bookings = $user->bookings()->with('checkIn','resource','resource.location')
                                 ->withTrashed()
                                 ->orderBy('date','asc')
                                 ->get();
}

奇怪的结果是,我仅获得第一条记录的关系数据,而没有获得第二条记录的关系数据(为便于阅读而缩写):

id: "bc7d2500-c779-11ea-9d49-c7bb70e58097"
check_in: {id: 1257,user_id: "b9189540-bf96-11ea-bfb2-fb5284940ca4",booking_id: "bc7d2500-c779-11ea-9d49-c7bb70e58097",check_in: "2020-07-22 10:14:18",check_out: null,created_at: "2020-07-22T08:14:18.000000Z",id: 1257,updated_at: "2020-07-22T08:14:18.000000Z",user_id: "b9189540-bf96-11ea-bfb2-fb5284940ca4"}
resource: {id: 4,name: "XXX",...} 
user_id: "b9189540-bf96-11ea-bfb2-fb5284940ca4"

id: "2e8749e0-ccd5-11ea-9b46-9febde327734"
check_in: null
resource: {id: 4,...} 
user_id: "b9189540-bf96-11ea-bfb2-fb5284940ca4"

一段时间后,我终于检查了Telescope以获取所提交的关系查询,并得到了:

select
  *
from
  `checkins`
where
  `checkins`.`booking_id` in (
    1,115,1661,19,1,100000,203,20,238,23,242,275,28820,29,2,385065,38,3980,3,43539880,56,5,64078300,6831,74575330,77,7,83,864465,8,9223372036854775807,900,918,92,95,961852,990,99,9,0
  )

这些ID是什么?我真的不知道该怎么办。我使用的UUID库是否有问题?但是,为什么第一个关系起作用?任何提示高度赞赏。预先感谢。

解决方法

如果您使用的是https://github.com/binarycabin/laravel-uuid,则需要对所有以uuid作为主键的表使用特征UUIDIsPrimaryKey,即

class Booking extends Model
{
    use SoftDeletes,HasUUID,UUIDIsPrimaryKey;

这可能与Eloquent model conventions中提到的$keyType属性相关

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...