本文介绍了在laravel中获取与自类的关系时出现非法偏移类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的User模型是这样的:

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');
    }

}

如您所见,在supervisor列中指定了用户的父母.

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

现在要获取具有supervisor= null的用户模型的所有子代,我这样写:

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

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

但它总是返回此错误:

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

表用户具有以下数据:

+---------+-------------+---------+--------------+------------+
| 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          |
+---------+-------------+---------+--------------+------------+

更新:

我发现问题是我有一个具有相同名称的访问器supervisor属性,如下所示:

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);
}

我补充说,因为我想返回 supervisor 用户作为对象.
但是现在在这种情况下,我该怎么办?

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

推荐答案

尝试使用此设置:

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

并像这样获取它们

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

如果这不能解决您的问题,则您的relationships定义不明确,那么我建议阅读有关children.children模型中的foreignlocal键的信息

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

注意:

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)

我想指出 The callback should return an associative array with a single key/value pair. ,这意味着您无法做到recursively,至少我相信您做不到.

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.

这篇关于在laravel中获取与自类的关系时出现非法偏移类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 23:17