如何解决Laravel关系“有时”返回null
整个应用程序运行平稳,我对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
属性相关
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。