简体   繁体   English

Laravel 一对多关系不起作用 - 返回递归

[英]Laravel One to Many relationship not working - returns recursion

I am developing an application with Laravel 8 and I ran into very strange behavior.我正在用 Laravel 8 开发一个应用程序,我遇到了非常奇怪的行为。

I have a Model called "Organisation", this Organisation has many Users (Model from Jetstream).我有一个名为“组织”的 Model,这个组织有很多用户(来自 Jetstream 的模型)。

I did the relationship as usual:我像往常一样处理关系:

In Organisation Model:在组织 Model 中:

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

In User Model:在用户 Model 中:

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

I have a field on the users table called organisation_id which is declared in the migration as such:我在用户表上有一个名为 organization_id 的字段,它在迁移中声明如下:

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

I checked the DB, everything is filled in, with no null values.我检查了数据库,所有内容都已填写,没有 null 值。

Expected result: If I call预期结果:如果我打电话

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

I will get the organization object.我会得到组织 object。

Actual result: I receive a veeeeery log object, stating something including RECURSION , instead of the organization I want.实际结果:我收到一个 veeeeery 日志 object,其中说明了包括RECURSION在内的内容,而不是我想要的组织。 Is this an error regarding the foreign key?这是关于外键的错误吗? The User model also has a belongsToMany Relationship to a different model, is this in the way of the standard belongsTo?用户 model 也与另一个 model 有 belongsToMany 关系,这是标准 belongsTo 的方式吗?

EDIT I can receive the organization when calling编辑我可以在打电话时收到组织

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

But this is not the clean Laravel way I want to work with.但这不是我想要使用的干净的 Laravel 方式。

I debugged the query with $testOrg=$testUser->organisation()->toSql();我用 $testOrg=$testUser->organisation()->toSql(); 调试查询and it displayed me: string(60) "select * from organisations where organisations . id =?",它向我显示:字符串(60)“从organisations中选择 * organisations id =?”,

So the "where" is wrong?那么“哪里”错了?

Any hints or help would be very appreciated任何提示或帮助将不胜感激

Here is some of the output: (whole output is too long)这是 output 的一部分:(整个 output 太长了)

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) { ["quer NULL [“ rememe_token”] => NULL [“ current_team_id”] => NULL [“ profile_photo_path”] => NULL [ 1)}[“变化”:受保护]=>数组(0){}[“classCastCache”:受保护]=>数组(0){}[“日期”:受保护]=>数组(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) { ["查询y":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) y":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 { ["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 "]=> 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]=>递归

Try this:试试这个:

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

If you want to continue your way then update to this:如果你想继续你的方式然后更新到这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM