草庐IT

Laravel 与 uuid 的多对多关系返回总是空的

codeneng 2023-03-28 原文

Laravel many to many relationship with uuid returns always empty

我使用 Laravel 5.8 并将模型的自动增量 ID 更改为 uuid。从那以后,我在我的两个模型 EventUser (使用数据透视表 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 :
Array
(
    [0] =>
)

有人知道我在这里想念什么吗?

这是我的模型的定义:

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()
{
    return $this
        ->belongsToMany(User::class, 'events_users', 'event_id', 'user_id')
        ->withTimestamps();
}

}

用户模型的声明相同,但有关系

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) {
        print_r($user); die; // It never loops here as its length is 0 but should be 2...
    }

    \\DB::listen(function ($query) {
        print_r($query->sql);
        print_r($query->bindings);
        // $query->time
    });

    $subscriptions = $event
        ->users()
        ->get();
    die;

    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 !
public $title       ="";
public $start_date  ="";
public $end_date    ="";
public $location    ="";
public $latitude    ="";
public $longitude   ="";

有关Laravel 与 uuid 的多对多关系返回总是空的的更多相关文章

  1. ruby - 为什么 4.1%2 使用 Ruby 返回 0.0999999999999996?但是 4.2%2==0.2 - 2

    为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返

  2. ruby - 为什么 SecureRandom.uuid 创建一个唯一的字符串? - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?

  3. ruby-on-rails - Rails - 乐观锁定总是触发 StaleObjectError 异常 - 2

    我正在学习Rails,并阅读了关于乐观锁的内容。我已将类型为integer的lock_version列添加到我的articles表中。但现在每当我第一次尝试更新记录时,我都会收到StaleObjectError异常。这是我的迁移:classAddLockVersionToArticle当我尝试通过Rails控制台更新文章时:article=Article.first=>#我这样做:article.title="newtitle"article.save我明白了:(0.3ms)begintransaction(0.3ms)UPDATE"articles"SET"title"='dwdwd

  4. ruby - 检查字符串是否包含散列中的任何键并返回它包含的键的值 - 2

    我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案

  5. ruby - Ruby 中的隐式返回值是怎么回事? - 2

    所以我开始关注ruby​​,很多东西看起来不错,但我对隐式return语句很反感。我理解默认情况下让所有内容返回self或nil但不是语句的最后一个值。对我来说,它看起来非常脆弱(尤其是)如果你正在使用一个不打算返回某些东西的方法(尤其是一个改变状态/破坏性方法的函数!),其他人可能最终依赖于一个返回对方法的目的并不重要,并且有很大的改变机会。隐式返回有什么意义?有没有办法让事情变得更简单?总是有返回以防止隐含返回被认为是好的做法吗?我是不是太担心这个了?附言当人们想要从方法中返回特定的东西时,他们是否经常使用隐式返回,这不是让你组中的其他人更容易破坏彼此的代码吗?当然,记录一切并给出

  6. ruby - Rails 关联 - 同一个类的多个 has_one 关系 - 2

    我的问题的一个例子是体育游戏。一场体育比赛有两支球队,一支主队和一支客队。我的事件记录模型如下:classTeam"Team"has_one:away_team,:class_name=>"Team"end我希望能够通过游戏访问一个团队,例如:Game.find(1).home_team但我收到一个单元化常量错误:Game::team。谁能告诉我我做错了什么?谢谢, 最佳答案 如果Gamehas_one:team那么Rails假设您的teams表有一个game_id列。不过,您想要的是games表有一个team_id列,在这种情况下

  7. ruby-on-rails - ruby 日期方程不返回预期的真值 - 2

    为什么以下不同?Time.now.end_of_day==Time.now.end_of_day-0.days#falseTime.now.end_of_day.to_s==Time.now.end_of_day-0.days.to_s#true 最佳答案 因为纳秒数不同:ruby-1.9.2-p180:014>(Time.now.end_of_day-0.days).nsec=>999999000ruby-1.9.2-p180:015>Time.now.end_of_day.nsec=>999999998

  8. ruby - 从 String#split 返回的零长度字符串 - 2

    在Ruby1.9.3(可能还有更早的版本,不确定)中,我试图弄清楚为什么Ruby的String#split方法会给我某些结果。我得到的结果似乎与我的预期相反。这是一个例子:"abcabc".split("b")#=>["a","ca","c"]"abcabc".split("a")#=>["","bc","bc"]"abcabc".split("c")#=>["ab","ab"]在这里,第一个示例返回的正是我所期望的。但在第二个示例中,我很困惑为什么#split返回零长度字符串作为返回数组的第一个值。这是什么原因呢?这是我所期望的:"abcabc".split("a")#=>["bc"

  9. [工业相机] 分辨率、精度和公差之间的关系 - 2

    📢博客主页:https://blog.csdn.net/weixin_43197380📢欢迎点赞👍收藏⭐留言📝如有错误敬请指正!📢本文由Loewen丶原创,首发于CSDN,转载注明出处🙉📢现在的付出,都会是一种沉淀,只为让你成为更好的人✨文章预览:一.分辨率(Resolution)1、工业相机的分辨率是如何定义的?2、工业相机的分辨率是如何选择的?二.精度(Accuracy)1、像素精度(PixelAccuracy)2、定位精度和重复定位精度(RepeatPrecision)三.公差(Tolerance)四.课后作业(Post-ClassExercises)视觉行业的初学者,甚至是做了1~2年

  10. ruby - 为什么 Integer.respond_to?( :even? ) 返回 false? - 2

    我一直在研究RubyKoans,我发现about_open_classes.rbkoan很有趣。特别是他们修改Integer#even?方法的最后一个测试。我想尝试一下这个概念,所以我打开了Irb并尝试运行Integer.respond_to?(:even?),但令我惊讶的是我得到了错误。然后我尝试了Fixnum.respond_to?(:even?)并得到了错误。我还尝试了Integer.respond_to?(:respond_to?)并得到了true,当我执行2.even?时,我也得到了true。我不知道发生了什么。谁能告诉我缺少什么? 最佳答案

随机推荐