laravel 获取多上级和多下级节点

admin 4个月前 157 阅读

自身无限层级,上级 ID 字段parent_ids,形式 1,2,3,4,5

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\Node;

class NodeController extends Controller
{
    public function item($alias)
    {
        $node = Node::where(['alias'=>$alias])->first();
        $node->parents = Node::where(['status'=>'1'])->whereIn('id',$node->parent_ids)->get();
        $nid = $node->id;
        $node->childs = Node::where(function ($query) use ($nid) {
                  $query->whereRaw("FIND_IN_SET($nid,parent_ids)");
              })->orderBy('created_at','DESC')->get();

        return view('node.node_item',compact('node'));
    }
}

$node->parents 上级
$node->childs 下级

回复 0
登录之后才能参与