Laravel Eloquent 模型即使在错误的条件下也能返回值



我没有遇到以下问题:

  1. $user = AppUser::where('id', 1)->first();作品

  2. $user = AppUser::where('id', '1')->first();工作 - 相同的结果

  3. $user = AppUser::where('id', '1xxxx')->first();工作 - 相同的结果

  4. $user = AppUser::where('id', 'x1')->first();不起作用

我对 2.感到惊讶,对 3 感到惊讶。有什么线索吗?

由于id列是int类型,它期望比较的int值和(int('1xxxx' = 1,所以所有这些结果都是预期的。

使用修补匠

>>> AppUser::where('id', '1xxxxx')->first();
=> AppUser {#3118
     id: 1,
     name: "superadmin",
     email: "abajaj281@yahoo.com",
     created_at: "2019-01-11 19:06:23",
     updated_at: "2019-01-11 19:06:23",
   }
>>> AppUser::find('1xxxxx');
=> AppUser {#3123
     id: 1,
     name: "superadmin",
     email: "abajaj281@yahoo.com",
     created_at: "2019-01-11 19:06:23",
     updated_at: "2019-01-11 19:06:23",
   }

    >>> AppUser::find('xxxxx1');
    => null
   Psy Shell v0.9.9 (PHP 7.2.10 — cli) by Justin Hileman
   >>> AppUser::where('id', 'xxxxx1')->first();
   => null
   >>> AppUser::where('id', 'x1')->first();
   => null
    >>>
   >>> AppUser::find('x1');
   => null
    >>>

发生这种情况是因为在后台,它根据列类型匹配value

作为int列,它将值转换为int形式,例如

https://3v4l.org/JkU9H

这意味着所有 3 点都匹配 value 1 ,而最后一个点匹配 0,因为无法将 "x" 转换为int

最新更新