Laravel返回兄弟姐妹导致非对象情况 [英] Laravel Return Siblings Results in Non-Object situation

查看:79
本文介绍了Laravel返回兄弟姐妹导致非对象情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个导入到数据库的用户表.我将其原始ID保留为reference_id:

I have a table of users that are imported to the DB. I keep their original id as reference_id:

id    |  reference_id   |   parent_id
1           35                null
2           36                 35
3           37                 35
4           38                null
5           37                 38

我正试图找回整个家庭.在laravel中,我在模型中使用了它:

I am trying to get back the entire family. In laravel I am using this in the model:

public function getFamily(){
    $parent = User::where('user_id', auth()->id())
    ->where('parent_id', $this->parent_id)->get();

    $children = User::where('user_id', auth()->id())
    ->where('reference_id', $this->parent_id)->get();

    return $parent->merge($children);
}

,然后在刀片服务器模板中这样调用它:

and then in the blade template calling it like this:

$family= $tasks->user->getFamily();

foreach($family as $member){
    print_r($member->appointments->some_field);
}

当我尝试调用非对象的print_r时出现错误,并且不确定如何处理.约会是指另一个模型/表.

I am getting an error when I do the print_r that I am trying to call on a non-object and I am not sure what to do about it. Appointments refers to another model / table.

在其余的User模型中,我可以使用以下类似的方法更简单地做到这一点:

In the rest of the User model I was able to do it more simply with something like this:

public function getFamily(){
    return $this->hasMany('App\Models\User', 'reference_id', 'parent_id');
        ->where('user_id', auth()->id())
        ->where('parent_id', $this->parent_id);
}

这个特殊的应该"在被调用时返回了所有同级兄弟:$family= $tasks->user->getFamily,然后我可以访问约会模型,但是我收到一个错误,我试图用1.6来耗尽内存Gb:

This particular one "should" have returned all the siblings when called like this: $family= $tasks->user->getFamily and then I could have accessed the Appointments Model, but I was getting an error that I was running out of memory trying to use about 1.6Gb:

[2018-02-14 00:25:15] local.ERROR: Symfony\Component\Debug\Exception\FatalErrorException: Allowed memory size of 2634022912 bytes exhausted (tried to allocate 1608531968 bytes) in /var/www/html/laravel/storage/framework/views/bcfe7a474602582174256aacc1597a287fcb4e5e.php:213

有什么方法可以使我的第一个工作功能适应我,也可以让我加入关联的表吗?除此以外,第二个查询中是否有明显的遗漏?

Is there any way to adapt my first and working function to also allow me to join on associated tables? Barring that, is there something obvious I am missing in the second query?

推荐答案

我将从将getFamily函数更改为然后,您应该可以通过以下方式访问所有Appointment模型:

You should then have access to all Appointment models via:

$appointments = User::family()->each->appointments;

修改

public function scopeFamily($query, $parentId) {
    if(isset($parentId)){
        return $query->where('user_id', auth()->id())->where(function ($query) use ($parentId) {
            $query->where('parent_id', $parentId)
                  ->orWhere('reference_id', $parentId);
        });
    }
}

这篇关于Laravel返回兄弟姐妹导致非对象情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆