我正在使用 Laravel 8 开发一个应用程序,但遇到了非常奇怪的行为。
我有一个名为“组织”的模型,该组织有许多用户(来自 Jetstream 的模型)。
我像往常一样建立了关系:
在组织模型中:
public function users()
{
return $this->hasMany(User::class);
}
在用户模型中:
public function organisation()
{
return $this->belongsTo(Organisation::class);
}
我的用户表中有一个名为organization_id的字段,它在迁移中声明如下:
$table->foreignId('organisation_id')->nullable()->constrained();
我检查了数据库,所有内容都已填写,没有空值。
预期结果: 如果我打电话
$testUser=User::find(1);
$testOrg=$testUser->organisation();
我将获取组织对象。
实际结果: 我收到一个 veeeeeery 日志对象,其中包含RECURSION,而不是我想要的组织。这是关于外键的错误吗? User 模型还与另一个模型有一个belongsToMany 关系,这是标准belongsTo 的方式吗?
编辑 我打电话时可以收到组织
$testUser=User::with('organisation')->find(1);
但这不是我想要的干净的 Laravel 工作方式。
我使用 $testOrg=$testUser->organization()->toSql() 调试了查询;
它向我展示了:
string(60)“从组织
中选择*,其中组织
。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;
关于php - Laravel 一对多关系不起作用 - 返回递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66239558/