Laravel 5.5检索嵌套关系

时间:2018-03-22 12:12:56

标签: php laravel orm relationship

如何在Eloquent查询构建器中对关系使用first()方法?

我只想返回该部分中的第一部分和第一部分。

我的评估服务中有以下查询:

public function firstSubsection()
    {
        return $this->model->with(['sections.subsections' => function ($q) {
                $q->take(1);
        }])->first();
    }

这会返回多个部分:

 {
        "id": 1,
        "name": "Assessment 1",
        "description": "<p>Some description</p>",
        "created_at": "2018-03-09 17:14:43",
        "updated_at": "2018-03-09 17:14:43",
        "sections": [
            {
                "id": 5,
                "name": "Section 1",
                "description": null,
                "parent": null,

                "created_at": "2018-03-09 17:14:52",
                "updated_at": "2018-03-09 17:14:52",
                "pivot": {
                    "assessment_id": 1,
                    "section_id": 5
                },
                "subsections": [
                    {
                        "id": 6,
                        "name": "Subsection 1",
                        "description": "<p>Lorem ipsum dolor sit amet<br>\r\n</p>",
                        "parent": 5,
                        "position": 6,
                        "created_at": "2018-03-09 17:15:08",
                        "updated_at": "2018-03-21 11:40:10"
                    }
                ]
            },
        {
            "id": 10,
            "name": "Section 2",
            "description": null,
            "parent": null,
            "position": 10,
            "created_at": "2018-03-20 14:34:50",
            "updated_at": "2018-03-20 14:34:50",
            "pivot": {
                "assessment_id": 1,
                "section_id": 10
            },
            "subsections": []
        }
    ]
}

知道如何实现这个目标吗?

我的Eloquent模型中有以下关系:

class Section extends Model
{
    use BelongsToSortedManyTrait, SortableTrait;

    public $fillable = [
        'id',
        'name',
        'description',
        'parent',
        'position'
    ];

    public static $rules = [
        // create rules
        'name' => 'required'
    ];

    public function assessments() {
        return $this->belongsToSortedMany('App\Models\Assessment');
    }

    public function subsections() {
        return $this->hasMany(self::class, 'parent')->sorted();
    }
}



class Assessment extends Model
{

    public $fillable = [
        'id',
        'name',
        'description'
    ];

    public static $rules = [
        // create rules
    ];

    public function sections()
    {
        return $this->belongsToMany('App\Models\Section')->whereNull('parent')->sorted();
    }


}

4 个答案:

答案 0 :(得分:0)

public function firstSubsection()
{
    return $this->model->with(['sections.subsections' => function ($q) 
    {
            $q->take(1)->first();
    }])->first();
}

答案 1 :(得分:0)

我不确定这是否有效,但你可以尝试这个逻辑:

如果您想急切加载模型的第一部分和子部分,您应该在模型中创建一个新的关系:

public function firstSectionOnly()
{
    return $this->hasOne(Section::class)->orderBy('id', 'asc');
}

部分模型中定义

public function firstSubSectionOnly()
{
    return $this->hasOne(Subsection::class)->orderBy('id', 'asc');
}

然后您可以在当前模型中急切加载它:

public function firstSubsection()
{
    return $this->model->with(['firstSectionOnly.firstSubSectionOnly'])->first();
}

尝试之后。告诉我它有效或有错误。

答案 2 :(得分:0)

Take(1)first()现在正在做同样的事情。但仍然不是问题。移除take(1)并在->get()

之后添加first()

试一试。

答案 3 :(得分:0)

最后我必须这样做:

public function firstSubsection()
    {
        return $this->model->with(['sections' => function ($q) {
            $q->with(['subsections' => function ($q) {
                $q->first();
            }
            ])->first();
        }])->first();
    }

感谢@ab_in引导我朝着正确的方向前进。