Php 如何在Laravel 5中链接递归数据scructure?

Php 如何在Laravel 5中链接递归数据scructure?,php,laravel,eloquent,recursive-datastructures,Php,Laravel,Eloquent,Recursive Datastructures,我正在制作一个具有递归结构的模型,如下所示。概念模型可以有父概念和子概念,并且模型可以按预期工作。我的问题是如何实现在两个概念之间添加链接的页面 <?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Concept extends Model { // public function parentConcepts() { return $this-&g

我正在制作一个具有递归结构的模型,如下所示。概念模型可以有父概念和子概念,并且模型可以按预期工作。我的问题是如何实现在两个概念之间添加链接的页面

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Concept extends Model
{
    //
    public function parentConcepts()
    {
        return $this->belongsToMany('App\Models\Concept','concept_concept','parent_concept_id','child_concept_id');
    }
    public function childConcepts()
    {
        return $this->belongsToMany('App\Models\Concept','concept_concept','child_concept_id','parent_concept_id');
    }
    public function processes()
    {
        return $this->hasMany('App\Models\Process');
    }
}

在控制器中创建一个利用attach()函数的新函数是此解决方案的关键

public function storeChildLink(Request $request)
{
    //get the parent concept id
    $concept = Concept::find($request->input('parentConceptId'));
    //attach the child concept
    $concept->childConcepts()->attach($request->input('childConceptId'));

    $concept->save();

    return redirect()->route('concept.show', ['id' => $request['parentConceptId']])->with('status', 'Child Concept Successfully Linked');
}

public function storeParentLink(Request $request)
{
    //get the child concept id
    $concept = Concept::find($request->input('childConceptId'));
    //attach the parent concept
    $concept->parentConcepts()->attach($request->input('parentConceptId'));

    $concept->save();

    return redirect()->route('concept.show', ['id' => $request['childConceptId']])->with('status', 'Parent Concept Successfully Linked');
}