【问题标题】:Laravel One to Many relationship not working - returns recursionLaravel 一对多关系不起作用 - 返回递归
【发布时间】:2021-05-20 04:49:14
【问题描述】:

我正在使用 Laravel 8 开发应用程序,但遇到了非常奇怪的行为。

我有一个名为“组织”的模型,这个组织有很多用户(来自 Jetstream 的模型)。

我照常处理关系:

在组织模型中:

    public function users()
    {
        return $this->hasMany(User::class);
    }

在用户模型中:

 public function organisation()
    {
        return $this->belongsTo(Organisation::class);
    }

我在 users 表上有一个名为 organization_id 的字段,它在迁移中声明如下:

 $table->foreignId('organisation_id')->nullable()->constrained();

我检查了数据库,所有内容都已填写,没有空值。

预期结果: 如果我打电话

  $testUser=User::find(1);
  $testOrg=$testUser->organisation();

我会得到组织对象。

实际结果: 我收到了一个古怪的日志对象,其中包含 RECURSION 在内的内容,而不是我想要的组织。这是关于外键的错误吗? User 模型也和不同的模型有一个 belongsToMany 关系,这是标准的 belongsTo 的方式吗?

编辑 打电话时可以接听组织

  $testUser=User::with('organisation')->find(1);

但这不是我想要使用的干净的 Laravel 方式。

我使用 $testOrg=$testUser->organisation()->toSql(); 调试了查询 它向我展示了: 字符串(60)“从organisations中选择*organisationsid=?” ,

所以“哪里”错了?

任何提示或帮助将不胜感激

这是部分输出:(整个输出太长了)

NULL ["remember_token"]=> NULL ["current_team_id"]=> NULL ["profile_photo_path"]=> NULL ["created_at"]=> NULL ["updated_at"]=> NULL ["organisation_id"]=> int(1) } ["changes":protected]=> array(0) { } ["classCastCache":protected]=> 数组(0) { } ["dates":protected]=> 数组(0) { } ["dateFormat":protected]=> NULL ["dispatchesEvents":protected]=> 数组(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["timestamps"]=> bool(true) ["visible":protected]=> array(0) { } ["guarded":protected]=> 数组(1) { [0]=> 字符串(1) "*" } ["rememberTokenName":protected]=> 字符串(14) "remember_token" ["accessToken":protected]=> NULL } ["foreignKey":protected]=> string(15) "organisation_id" ["ownerKey":protected]=> string(2) "id" ["relationName":protected]=> string(12) "组织" [“查询”:受保护]=> object(Illuminate\Database\Eloquent\Builder)#1386 (8) { ["query":protected]=> 对象(Illuminate\Database\Query\Builder)#1388 (22) { [“连接”]=> 对象(Illuminate\Database\MySqlConnection)#1353 (18) { ["pdo":protected]=> 对象(PDO)#1364 (0) { } ["readPdo":protected]=> NULL [“数据库”:受保护]=> 字符串(7)“laravel” ["tablePrefix":protected]=> 字符串(0) "" ["config":protected]=> 数组(15){[“驱动程序”]=>字符串(5)“mysql”[“主机”]=>字符串(9) "127.0.0.1" ["端口"]=> 字符串(4) "3306" ["数据库"]=> 字符串(7) "laravel" ["用户名"]=> 字符串(4) "root" ["密码"]=> 字符串(0) "" ["unix_socket"]=> 字符串(0) "" ["charset"]=> 字符串(7) "utf8mb4" ["collat​​ion"]=> 字符串(18) "utf8mb4_unicode_ci" ["prefix"]=> 字符串(0) "" ["prefix_indexes"]=> bool(true) ["strict"]=> bool(true) ["engine"]=> NULL ["options"]=> array(0) { } ["name"]=> string(5) "mysql" } ["reconnector":protected]=> object(Closure)#132 (2) { ["this"]=> 对象(Illuminate\Database\DatabaseManager)#40 (5) { ["app":protected]=> 对象(Illuminate\Foundation\Application)#2 (35) { ["basePath":protected]=> 字符串(38) “C:\xampp\htdocs\wiederverkaufen 门户” ["hasBeenBootstrapped":protected]=> bool(true) ["booted":protected]=> bool(true) ["bootingCallbacks":protected]=> 数组(2) { [0]=> object(Closure)#195 (2) { ["static"]=> array(1) { ["instance"]=> object(Illuminate\Queue\QueueServiceProvider)#189 (3) { ["app":protected]=> 递归 ["bootingCallbacks":protected]=> array(0) { } ["bootedCallbacks":protected]=> array(0) { } } } ["this"]=> RECURSION } [1]=> object(Closure)#338 (2) { ["static"]=> 数组(1){[“实例”]=> object(Illuminate\Cache\CacheServiceProvider)#332 (3) { ["app":protected]=> 递归 ["bootingCallbacks":protected]=> array(0) { } ["bootedCallbacks":protected]=> array(0) { } } } ["this"]=> RECURSION } } ["bootedCallbacks":protected]=> array(1) { [0]=> 对象(闭包)#340 (1) { ["this"]=> 对象(App\Providers\RouteServiceProvider)#164 (5) { ["namespace":protected]=> NULL ["loadRoutesUsing":protected]=> object(Closure)#341 (1) { ["this"]=> RECURSION } ["app":protected]=> RECURSION ["bootingCallbacks":protected]=> array(0) { } ["bootedCallbacks":protected]=> array(1) { [0]=> object(Closure)#165 (1) { ["this"]=> 递归 } } } } } ["terminatingCallbacks":protected]=> array(0) { } ["serviceProviders":protected]=> 数组(34) { [0]=> 对象(Illuminate\Events\EventServiceProvider)#6 (3) { ["app":protected]=> 递归 ["bootingCallbacks":protected]=> array(0) { } ["bootedCallbacks":protected]=> array(0) { } } [1]=> object(Illuminate\Log\LogServiceProvider)#8 (3) { ["app":protected]=> 递归

【问题讨论】:

    标签: php laravel eloquent jetstream


    【解决方案1】:

    试试这个:

     $testUser=User::with('organisation')->find(1);
    

    如果你想继续你的方式,那么更新到这个:

    $testUser=User::find(1);
    $testOrg=$testUser->organisation;
    

    【讨论】:

    • 这确实有效,我得到了组织。但它不应该通过调用 $testUser->organisation() 来工作吗?
    • 不,这样不行,尝试使用 laravel 急切加载,根据 laravel 文档,这是最好的方法。
    • 但是在 Laravel 文档中,您可以轻松访问 belongsTo 关系的父级,就像我在这里所做的那样:laravel.com/docs/8.x/eloquent-relationships#one-to-many-inverse
    • 您将组织()更新为组织时出错。只需删除括号。
    • 没问题,很高兴为您提供帮助。
    猜你喜欢
    • 2020-11-03
    • 2020-03-14
    • 2018-07-05
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 2012-11-26
    • 2019-07-24
    相关资源
    最近更新 更多