Laravel 一对多关系无效 - returns 递归

Laravel One to Many relationship not working - returns recursion

我正在使用 Laravel 8 开发一个应用程序,我 运行 的行为非常难运行。

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

我照常做关系:

在组织模型中:

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

在用户模型中:

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

我在用户 table 上有一个名为 organisation_id 的字段,它在迁移中声明为:

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

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

预期结果: 如果我打电话

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

我会得到组织对象。

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

编辑 打电话就能接到组织

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

但这不是我想要的干净的Laravel工作方式。

我用 $testOrg=$testUser->organisation()->toSql(); 调试了查询; 它向我展示了: string(60) “select * 来自 organisations 其中 organisations.id = ?” ,

所以“哪里”是错的?

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

这是一些输出:(整个输出太长)

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]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["dispatchesEvents":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["timestamps"]=> bool(true) ["visible":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } ["rememberTokenName":protected]=> string(14) "remember_token" ["accessToken":protected]=> NULL } ["foreignKey":protected]=> string(15) "organisation_id" ["ownerKey":protected]=> string(2) "id" ["relationName":protected]=> string(12) "organisation" ["query":protected]=> object(Illuminate\Database\Eloquent\Builder)#1386 (8) { ["query":protected]=> object(Illuminate\Database\Query\Builder)#1388 (22) { ["connection"]=> object(Illuminate\Database\MySqlConnection)#1353 (18) { ["pdo":protected]=> object(PDO)#1364 (0) { } ["readPdo":protected]=> NULL ["database":protected]=> string(7) "laravel" ["tablePrefix":protected]=> string(0) "" ["config":protected]=> array(15) { ["driver"]=> string(5) "mysql" ["host"]=> string(9) "127.0.0.1" ["port"]=> string(4) "3306" ["database"]=> string(7) "laravel" ["username"]=> string(4) "root" ["password"]=> string(0) "" ["unix_socket"]=> string(0) "" ["charset"]=> string(7) "utf8mb4" ["collation"]=> string(18) "utf8mb4_unicode_ci" ["prefix"]=> string(0) "" ["prefix_indexes"]=> bool(true) ["strict"]=> bool(true) ["engine"]=> NULL ["options"]=> array(0) { } ["name"]=> string(5) "mysql" } ["reconnector":protected]=> object(Closure)#132 (2) { ["this"]=> object(Illuminate\Database\DatabaseManager)#40 (5) { ["app":protected]=> object(Illuminate\Foundation\Application)#2 (35) { ["basePath":protected]=> string(38) "C:\xampp\htdocs\wiederverkaufen-portal" ["hasBeenBootstrapped":protected]=> bool(true) ["booted":protected]=> bool(true) ["bootingCallbacks":protected]=> array(2) { [0]=> object(Closure)#195 (2) { ["static"]=> array(1) { ["instance"]=> object(Illuminate\Queue\QueueServiceProvider)#189 (3) { ["app":protected]=> RECURSION ["bootingCallbacks":protected]=> array(0) { } ["bootedCallbacks":protected]=> array(0) { } } } ["this"]=> RECURSION } [1]=> object(Closure)#338 (2) { ["static"]=> array(1) { ["instance"]=> object(Illuminate\Cache\CacheServiceProvider)#332 (3) { ["app":protected]=> RECURSION ["bootingCallbacks":protected]=> array(0) { } ["bootedCallbacks":protected]=> array(0) { } } } ["this"]=> RECURSION } } ["bootedCallbacks":protected]=> array(1) { [0]=> object(Closure)#340 (1) { ["this"]=> object(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"]=> RECURSION } } } } } ["terminatingCallbacks":protected]=> array(0) { } ["serviceProviders":protected]=> array(34) { [0]=> object(Illuminate\Events\EventServiceProvider)#6 (3) { ["app":protected]=> RECURSION ["bootingCallbacks":protected]=> array(0) { } ["bootedCallbacks":protected]=> array(0) { } } [1]=> object(Illuminate\Log\LogServiceProvider)#8 (3) { ["app":protected]=> RECURSION

试试这个:

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

如果您想继续您的方式,请更新至此:

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