Laravel 关系 "sometimes" returns 空

Laravel relation "sometimes" returns null

我有一个 Laravel 关系的 st运行ge 问题。

整个应用程序 运行 很顺利,我对 Laravel 中的关系有一定的了解,但最近出于统计原因我开始调查一些应用程序数据 运行以下问题。

我在 table:

有签到活动
-----------------------------------------------------------------------------------------------------------------------
|  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     |
-----------------------------------------------------------------------------------------------------------------------

附带的预订table是用外键设置的,记录存在。

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');
    }

    ...

我现在 运行 一个 eloquent 查询:

$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, name: "XXX", ...} 
user_id: "b9189540-bf96-11ea-bfb2-fb5284940ca4"

一段时间后,我终于检查了 Telescope 中提交的关系查询并得到了这个:

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

这些 ID 是什么?我真的不知道该怎么办。这是我使用的 UUID 库的问题吗?但是,为什么第一个关系有效?任何提示都非常感谢。提前致谢。

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

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

这可能与 Eloquent model conventions

中提到的 $keyType 属性有关