Search code examples
laraveltreeviewhreflaravel-5.4

Bootstrap Treeview links with laravel 5.4


I am using laravel 5.4 and Bootstrap Treeview. I have enabled the node text as link:

  $('#treeview').treeview({data: data, enableLinks: true});

and in the href field of each node in the Json array I have the laravel route

 "{{ route('opciones.create') }}"

Here is where I create the tree (I encode this to Json format in another part of my code)

function buildTree(array $elements, $parentId) {
    $branch = array();

    foreach ($elements as $element) {

     $element['text'] = $element['descripcion'];
     $element['href'] = "{{ route('opciones.create') }}";
     if ($element['padre'] == $parentId){
            $nodes = buildTree($elements, $element['id']);
            if ($nodes) {
                $element['nodes'] = $nodes;
            }

            $branch[] = $element;
        }
    }

    return $branch;
}

This is the function in the controller that I need to call which returns a view (a blade file)

  public function create()
    {    $opcionespadre = Opcion::where('tipo', '=', 'SUBMENU')->get();
         return view($this->path.'.create', compact('opcionespadre'));
    }

I am using this especific function just to test the tree nodes links, I have other functions in the controller where I need to send a parameter, but first I need to make the link goes somewhere.

When I click on the text of the node, it says that the page I am looking for doesn't exist. This is what appears in the browser bar:

http://127.0.0.1:8000/{{route('opciones.create')}}

What should I do?


Solution

  • The problem is in this line :

    $element['href'] = "{{ route('opciones.create') }}";
    

    Do it like this :

    function buildTree(array $elements, $parentId) {
        $branch = array();
    
        foreach ($elements as $element) {
    
         $element['text'] = $element['descripcion'];
         $element['href'] = route('opciones.create');
         if ($element['padre'] == $parentId){
                $nodes = buildTree($elements, $element['id']);
                if ($nodes) {
                    $element['nodes'] = $nodes;
                }
    
                $branch[] = $element;
            }
        }
    
        return $branch;
    }