Laravel many to many relationship with uuid returns always empty
我使用 Laravel 5.8 并将模型的自动增量 ID 更改为 uuid。从那以后,我在我的两个模型 Event 和 User (使用数据透视表 events_users)之间定义的多对多关系遇到了一些问题。
问题:
现在,当我请求加入两个表的所有元素(我的数据透视表中有 2 条记录)时,我总是得到一个空数组。
在调试 sql 时,我看到未设置 where 子句参数:
1
2 3 4 5 6 7 8 9 10 11 |
// Generated sql
select `users`.*, `events_users`.`event_id` as `pivot_event_id`, `events_users`.`user_id` as `pivot_user_id`, `events_users`.`created_at` as `pivot_created_at`, `events_users`.`updated_at` as `pivot_updated_at` from `users` inner join `events_users` on `users`.`id` = `events_users`.`user_id` where `events_users`.`event_id` = ? // Bindings : |
有人知道我在这里想念什么吗?
这是我的模型的定义:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Event extends Model
{ protected $primaryKey = ‘id’; protected $keyType = ‘string’; public $incrementing = false; // here some other model methods, fillable property, etc.
public function users() |
}
用户模型的声明相同,但有关系
1
2 3 4 5 6 |
public function events()
{ return $this ->belongsToMany(Event::class, ‘events_users’, ‘user_id’, ‘event_id’) ->withPivot([‘created_at’, ‘updated_at’]); } |
然后我使用 :
从服务中检索关系
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public function getSubscriptions($eventId)
{ $eventId = ‘a1b7c5d6-8f86-44f4-f31a-46e32917d5c0’; // for debug purpose only $event = Event::find($eventId); foreach ($event->users as $user) { \\DB::listen(function ($query) { $subscriptions = $event return $subscriptions; |
我的数据库包含记录
- 是否还有其他东西会破坏模型的行为?我基本上以 1:1 的比例复制了您的代码,而且它在我这边工作得非常好。
- 有趣,我不知道从哪里开始寻找这个,但我会检查,Tx
- 可能是覆盖您的 $event->users 属性的东西。我附上了我的数据库结构和代码来测试这个:hastebin.com/ebonawiguj.js
-
问题在于我的模型中列出了属性的另一个声明。
我在那里初始化了一个 id 属性,它可能与 uuid 类型冲突,或者我不知道究竟是什么导致了这出戏……
无论如何,删除此行让应用程序正常工作。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20/**
* @var array
* Rules used for fields validation
*/
public $rules = array(
‘title’ => ‘required|string|max:255’,
‘start_date’ => ‘required|date|date_format:Y-m-d’,
‘end_date’ => ‘required|date|date_format:Y-m-d|after_or_equal:start_date’,
‘location’ => ‘string|max:254’,
‘latitude’ => ‘numeric’,
‘longitude’ => ‘numeric’
);public $id =””; // This is the line that create the bug… Remove it and it works !
public $title =””;
public $start_date =””;
public $end_date =””;
public $location =””;
public $latitude =””;
public $longitude =””; -
问题在于我的模型中列出了属性的另一个声明。
我在那里初始化了一个 id 属性,它可能与 uuid 类型冲突,或者我不知道究竟是什么导致了这出戏……
无论如何,删除此行让应用程序正常工作。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20/**
* @var array
* Rules used for fields validation
*/
public $rules = array(
‘title’ => ‘required|string|max:255’,
‘start_date’ => ‘required|date|date_format:Y-m-d’,
‘end_date’ => ‘required|date|date_format:Y-m-d|after_or_equal:start_date’,
‘location’ => ‘string|max:254’,
‘latitude’ => ‘numeric’,
‘longitude’ => ‘numeric’
);public $id =””; // This is the line that create the bug… Remove it and it works !
public $title =””;
public $start_date =””;
public $end_date =””;
public $location =””;
public $latitude =””;
public $longitude =””; -
问题在于我的模型中列出了属性的另一个声明。
我在那里初始化了一个 id 属性,它可能与 uuid 类型冲突,或者我不知道究竟是什么导致了这出戏……
无论如何,删除此行让应用程序正常工作。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20/**
* @var array
* Rules used for fields validation
*/
public $rules = array(
‘title’ => ‘required|string|max:255’,
‘start_date’ => ‘required|date|date_format:Y-m-d’,
‘end_date’ => ‘required|date|date_format:Y-m-d|after_or_equal:start_date’,
‘location’ => ‘string|max:254’,
‘latitude’ => ‘numeric’,
‘longitude’ => ‘numeric’
);public $id =””; // This is the line that create the bug… Remove it and it works !
public $title =””;
public $start_date =””;
public $end_date =””;
public $location =””;
public $latitude =””;
public $longitude =””; -
问题在于我的模型中列出了属性的另一个声明。
我在那里初始化了一个 id 属性,它可能与 uuid 类型冲突,或者我不知道究竟是什么导致了这出戏……
无论如何,删除此行让应用程序正常工作。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20/**
* @var array
* Rules used for fields validation
*/
public $rules = array(
‘title’ => ‘required|string|max:255’,
‘start_date’ => ‘required|date|date_format:Y-m-d’,
‘end_date’ => ‘required|date|date_format:Y-m-d|after_or_equal:start_date’,
‘location’ => ‘string|max:254’,
‘latitude’ => ‘numeric’,
‘longitude’ => ‘numeric’
);public $id =””; // This is the line that create the bug… Remove it and it works !
public $title =””;
public $start_date =””;
public $end_date =””;
public $location =””;
public $latitude =””;
public $longitude =””; -
问题在于我的模型中列出了属性的另一个声明。
我在那里初始化了一个 id 属性,它可能与 uuid 类型冲突,或者我不知道究竟是什么导致了这出戏……
无论如何,删除此行让应用程序正常工作。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20/**
* @var array
* Rules used for fields validation
*/
public $rules = array(
‘title’ => ‘required|string|max:255’,
‘start_date’ => ‘required|date|date_format:Y-m-d’,
‘end_date’ => ‘required|date|date_format:Y-m-d|after_or_equal:start_date’,
‘location’ => ‘string|max:254’,
‘latitude’ => ‘numeric’,
‘longitude’ => ‘numeric’
);public $id =””; // This is the line that create the bug… Remove it and it works !
public $title =””;
public $start_date =””;
public $end_date =””;
public $location =””;
public $latitude =””;
public $longitude =””;
问题在于我的模型中列出了属性的另一个声明。
我在那里初始化了一个 id 属性,它可能与 uuid 类型冲突,或者我不知道究竟是什么导致了这出戏……
无论如何,删除此行让应用程序正常工作。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
/**
* @var array * Rules used for fields validation */ public $rules = array( ‘title’ => ‘required|string|max:255’, ‘start_date’ => ‘required|date|date_format:Y-m-d’, ‘end_date’ => ‘required|date|date_format:Y-m-d|after_or_equal:start_date’, ‘location’ => ‘string|max:254’, ‘latitude’ => ‘numeric’, ‘longitude’ => ‘numeric’ ); public $id =””; // This is the line that create the bug… Remove it and it works ! |
来源:https://www.codenong.com/56123972/