Php Laravel一对多关系不起作用-返回递归
我正在用Laravel8开发一个应用程序,我遇到了非常奇怪的行为 我有一个叫做Organization的模型,这个组织有许多来自Jetstream的用户模型 我像往常一样处理这段关系: 在组织模式中:Php Laravel一对多关系不起作用-返回递归,php,laravel,eloquent,jetstream,Php,Laravel,Eloquent,Jetstream,我正在用Laravel8开发一个应用程序,我遇到了非常奇怪的行为 我有一个叫做Organization的模型,这个组织有许多来自Jetstream的用户模型 我像往常一样处理这段关系: 在组织模式中: public function users() { return $this->hasMany(User::class); } 在用户模型中: public function organisation() { return
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();
我将获取组织对象
实际结果:
我接收到一个Veeeery日志对象,它声明了包括递归在内的内容,而不是我想要的组织。这是关于外键的错误吗?用户模型也与不同的模型有归属关系,这是标准的归属关系吗
编辑
我可以在打电话时接收组织
$testUser=User::with('organisation')->find(1);
但这不是我想要的干净的拉威尔方式
我用$testOrg=$testUser->organization->toSql调试了查询;
它向我展示了:
string60从Organizations.id=?,的组织中选择*
那么问题出在哪里呢
任何提示或帮助都将不胜感激
以下是一些输出:整个输出太长
NULL[记住\u令牌]=>NULL[当前\u团队\u id]=>NULL
[profile\u photo\u path]=>NULL[创建时间]=>NULL[更新时间]=>
NULL[Organization_id]=>int1}[changes:protected]=>array0{
}[classCastCache:protected]=>array0{}[dates:protected]=>
array0{}[dateFormat:protected]=>NULL
[dispatchesEvents:protected]=>array0{}
[可观察对象:受保护]=>array0{}[关系:受保护]=>
array0{}[Touchs:protected]=>array0{}[timestamps]=>
booltrue[visible:protected]=>array0{}
[guarded:protected]=>array1{[0]=>string1*}
[MemberTokenName:protected]=>string14记住\u标记
[accessToken:protected]=>NULL}[foreignKey:protected]=>
string15组织\u id[所有者密钥:受保护]=>string2 id
[relationName:protected]=>string12组织
[查询:受保护]=>
objectIlluminate\Database\Eloquent\Builder1386 8{
[query:protected]=>objectIlluminate\Database\query\Builder1388
22{[连接]=>
objectIlluminate\Database\MySqlConnection1353 18{
[pdo:protected]=>objectPDO1364 0{}[readPdo:protected]=>
NULL[数据库:受保护]=>string7 laravel
[tablePrefix:protected]=>string0[config:protected]=>
array15{[driver]=>string5 mysql[host]=>string9
127.0.0.1[端口]=>string4 3306[数据库]=>string7
laravel[用户名]=>string4根[密码]=>string0
[unix_socket]=>string0[charset]=>string7 utf8mb4
[排序规则]=>string18 utf8mb4\u unicode\u ci[前缀]=>string0
[prefix_index]=>booltrue[strict]=>booltrue
[engine]=>NULL[options]=>array0{}[name]=>string5
mysql}[reconnector:protected]=>objectClosure132 2{
[此]=>ObjectillLuminate\Database\DatabaseManager 40 5{
[应用程序:受保护]=>objectIlluminate\Foundation\Application2 35{
[基本路径:受保护]=>string38
C:\xampp\htdocs\wiederverkaufen门户
[hasBeenBootstrapped:protected]=>booltrue[booted:protected]=>
booltrue[bootingCallbacks:protected]=>array2{[0]=>
ObjectClosure1952{[static]=>array1{[instance]=>
objectIlluminate\Queue\QueueServiceProvider189 3{
[应用程序:受保护]=>递归[引导回调:受保护]=>
array0{}[bootedCallbacks:protected]=>array0{}}
[this]=>RECURSION}[1]=>objectClosure338 2{[static]=>
数组1{[实例]=>
objectIlluminate\Cache\CacheServiceProvider332 3{
[应用程序:受保护]=>递归[引导回调:受保护]=>
array0{}[bootedCallbacks:protected]=>array0{}}
[this]=>RECURSION}}[bootedCallbacks:protected]=>array1{
[0]=>objectClosure340 1{[此]=>
objectApp\Providers\RouteServiceProvider164 5{
[命名空间:受保护]=>NULL[加载路由使用:受保护]=>
objectClosure341 1{[this]=>RECURSION}[app:protected]=>
递归[bootingCallbacks:protected]=>array0{}[bootedCallbacks:protected]=>array1{[0]=>objectClosure165
1{[this]=>递归}}
[终止回调:受保护]=>array0{}
[serviceProviders:受保护]=>array34{[0]=>
objectIlluminate\Events\EventServiceProvider6 3{
[应用程序:受保护]=>递归[引导回调:受保护]=>
array0{}[bootedCallbacks:protected]=>array0{}[1]=>
objectIlluminate\Log\LogServiceProvider8 3{[app:protected]=>
递归
试试这个:
$testUser=User::with('organisation')->find(1);
如果您想继续您的方式,请更新以下内容:
$testUser=User::find(1);
$testOrg=$testUser->organisation;
这是可行的,我得到了组织。但是,仅仅调用$testUser->Organization难道不可行吗?不,这样不行。尝试使用laravel Earge loading,这是根据laravel文档进行操作的最佳方式。但是在laravel文档中,您可以轻松访问belongsTo关系的父级,就像我在这里做的那样:Yo你犯了一个错误,将组织更新到另一个组织。只需删除括号。没问题,我很乐意帮助你。