简体   繁体   English

如何从具有父子关系的模型中获取下一个和上一个记录

[英]How to get next and previous records from a model with a parent child relationship

I have a parent child relationship in my model where a Section can have many subsections. 我的模型中有一个父子关系,其中一个Section可以有很多子节。 so in my model I have joined the table onto itself and using a parent column I can determine which section is a parent / child. 因此,在我的模型中,我已将表连接到自身上,并使用parent列可以确定哪个部分是父/子。

My question is how would I retrieve the next and previous records for the subsections? 我的问题是如何检索这些小节的下一个和上一个记录?

My Section model: 我的Section模型:

    class Section extends Model
    {
        use BelongsToSortedManyTrait, SortableTrait;


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


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

       public function next(){
        // get next record
        return self::where('id', '>', $this->id)->orderBy('id','asc')->first();

       }

       public  function previous(){
        // get previous record
        return self::where('id', '<', $this->id)->orderBy('id','desc')->first();

    }

    }

Notice the previous and next methods, at the moment they will work for all sections but won't take into account the parent / child relationship. 注意上一个和下一个方法,目前它们适用于所有部分,但不会考虑父/子关系。

Any way I can achieve this? 有什么办法可以做到这一点?

This has not been tested but try: 尚未测试,但请尝试:

self::where('parent', $this->parent)
    ->where('id', '<', $this->id)
    ->orderBy('id','desc')
    ->first();

Adding the parent to the query will restrict the next value to those with the same parent ID. 将父项添加到查询会将下一个值限制为具有相同父项ID的值。 You can then use like this: 然后可以这样使用:

$subsection = Section::find(2);
$next = $subsection->next();
$prev = $subsection->previous();

Furthermore, it may be better to make next and previous parent query optional: 此外,最好将下一个和上一个父查询设为可选:

public function next($parent = false) {
    $query = self::query();

    if ($parent) {
        $query = $query->where('parent', $this->parent);
    }

    $query = $query->where('id', '<', $this->id)
    ->orderBy('id','desc')
    ->first();
}

This will allow you to check the next and previous section as well as subsections. 这将使您可以检查下一部分和上一部分以及子部分。

All the best. 祝一切顺利。

Update (API solution - this has not been tested) 更新 (API解决方案-此未经测试)

$section = Section::find($id); // Whatever the section id is;

$subsection = Section::where('parent', $section->id)->first();

return [
    'section'         => $section,
    'next_section'    => $section->next()->id,
    'prev_section'    => $section->prev()->id,
    'subsection'      => $subsection,
    'next_subsection' => $subsection->next(true)->id,
    'prev_subsection' => $subsection->prev(true)->id,
];

On the next section call, you can then pass the subsection id and run next/prev before returning the api: 在下一节的调用中,您可以传递小节ID并在返回api之前运行next / prev:

$subsection = Section::find(1)->next() ; $subsection = Section::find(1)->next() ;

Try this: 尝试这个:

public function next(){
    $id = $this->parent ?: $this->id;
    return self::where('id', '>', $id)->orderBy('id','asc')->first();
}

public  function previous(){
    $id = $this->parent ?: $this->id;
    return self::where('id', '<', $id)->orderBy('id','desc')->first();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM