Skip to content
Advertisement

Laravel One to Many relationship not working – returns recursion

I am developing an application with Laravel 8 and I ran into very strange behavior.

I have a Model called “Organisation”, this Organisation has many Users (Model from Jetstream).

I did the relationship as usual:

In Organisation Model:

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

In User 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:

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

I checked the DB, everything is filled in, with no null values.

Expected result: If I call

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

I will get the organization object.

Actual result: I receive a veeeeery log object, stating something including RECURSION, instead of the organization I want. 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?

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.

I debugged the query with $testOrg=$testUser->organisation()->toSql(); and it displayed me: string(60) “select * from organisations where 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)

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(IlluminateDatabaseEloquentBuilder)#1386 (8) { [“query”:protected]=> object(IlluminateDatabaseQueryBuilder)#1388 (22) { [“connection”]=> object(IlluminateDatabaseMySqlConnection)#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(IlluminateDatabaseDatabaseManager)#40 (5) { [“app”:protected]=> object(IlluminateFoundationApplication)#2 (35) { [“basePath”:protected]=> string(38) “C:xampphtdocswiederverkaufen-portal” [“hasBeenBootstrapped”:protected]=> bool(true) [“booted”:protected]=> bool(true) [“bootingCallbacks”:protected]=> array(2) { [0]=> object(Closure)#195 (2) { [“static”]=> array(1) { [“instance”]=> object(IlluminateQueueQueueServiceProvider)#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(IlluminateCacheCacheServiceProvider)#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(AppProvidersRouteServiceProvider)#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(IlluminateEventsEventServiceProvider)#6 (3) { [“app”:protected]=> RECURSION [“bootingCallbacks”:protected]=> array(0) { } [“bootedCallbacks”:protected]=> array(0) { } } [1]=> object(IlluminateLogLogServiceProvider)#8 (3) { [“app”:protected]=> RECURSION

Advertisement

Answer

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;
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement