1

My User model is like this :

class User extends Authenticatable
{
    protected $primaryKey = 'user_id';
    protected $fillable = [
        'username',
        'password',
        'name',
        'family',
        'supervisor'
    ];

    public function children()
    {
        return $this->hasMany(\App\User::class, 'supervisor', 'user_id')->with('children');
    }

}

As you can see there a supervisor column that specify parent of a user.

Now to fetch all children of user models that have supervisor= null, I wrote this :

return User::with('children')->whereNull('supervisor')->get();

but it return this error always :

PHP Warning:  Illegal offset type in D:\wamp\www\zarsam-app\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Relations\HasOneOrMany.php on line 168

Table users have these data :

+---------+-------------+---------+--------------+------------+
| user_id |  username   |  name   |    family    | supervisor |
+---------+-------------+---------+--------------+------------+
|       1 | 09139616246 | ahmad   | badpey       | null       |
|       7 | alinasiri   | ali     | nasiri arani | 1          |
|       8 | zahedi      | mostafa | zahedi       | 1          |
|       9 | hsan        | hasan   | ghanati      | 8          |
+---------+-------------+---------+--------------+------------+

Update :

I found that problem is that I have a accessor same name supervisor attribute like this :

public function getSupervisorAttribute($value)
{
    return is_null($value) ? null : User::select('user_id', 'name', 'family')->find($value);
}

I added that because I want to return supervisor user as an object.
But now in this case, what do I do ?

Ahmad Badpey
  • 6,348
  • 16
  • 93
  • 159
  • Why do you use `->with('children')` in your `children` relation? – yrv16 Feb 13 '18 at 14:16
  • for my code to be recursive. based on https://stackoverflow.com/a/35768829/498504 – Ahmad Badpey Feb 13 '18 at 14:17
  • Either confirm your version of Laravel or show line 168 of HasOneOrMany.php so we can see what Eloquent is trying to do. – Devon Bessemer Feb 13 '18 at 14:19
  • I'm using `laravel 5.5.21`. this is line 168 : `protected function buildDictionary(Collection $results) { $foreign = $this->getForeignKeyName(); return $results->mapToDictionary(function ($result) use ($foreign) { return [$result->{$foreign} => $result]; // line 168 })->all();}` – Ahmad Badpey Feb 13 '18 at 14:22

1 Answers1

0

Try with this setup:

public function children()
{
    return $this->hasMany(\App\User::class, 'supervisor', 'user_id'); 
}

And fetching them like

return User::with('children.children')->whereNull('supervisor')->get();

If this doesn't resolve your issue, then your relationships are not well defined, then I suggest reading about foreignand local keys inside your children.children model

NOTE:

HasOneOrMany.php

...
return $results->mapToDictionary(function ($result) use ($foreign) { //line in question
...

And when I found that method, this is what it said:

/**
 * Run a dictionary map over the items.
 *
 * The callback should return an associative array with a single key/value pair.
 *
 * @param  callable  $callback
 * @return static
 */
public function mapToDictionary(callable $callback)

I want to point out The callback should return an associative array with a single key/value pair. Which means you cannot do it recursively, at least I believe you can't.

Nikola Gavric
  • 3,507
  • 1
  • 8
  • 16