mardi 30 janvier 2018

Laravel multilevel menu doesn't work

I have created multilevel menu by this example (first reply): How to create a database-driven multi-level navigation menu using Laravel

I'm always getting empty array. Here's my database structure (database's name is also "structure")

enter image description here

structure_id and parent_id are relevant.

Here's my code:

App/navigation.php

<?php
namespace App;

use DB;
use Illuminate\Database\Eloquent\Model;

class Navigation extends Model {

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'structure';

    public function parent() {

        return $this->hasOne('structure', 'structure_id', 'parent_id');

    }

    public function children() {

        return $this->hasMany('structure', 'parent_id', 'structure_id');

    }

    public static function tree() {

        return static::with(implode('.', array_fill(0, 4, 'children')))->where('parent_id', '=', NULL)->get();

    }

}

and the Controller:

app/http/controllers/structurecontroller.php

<?php

namespace App\Http\Controllers\Superuser;

use App\Http\Controllers\Controller;
use App\Navigation;
use App\Structure as Model;

class StructureController extends Controller
{
    /**
     * Allow only for logged in
     * DashboardController constructor.
     */
    public function __construct()
    {
        $this->middleware('auth');
    }


    public function index()
    {
        //$model = new Model();
        //$items = $model->getStructure();

        $items = Navigation::tree();
        var_dump($items);exit;



        return view('superuser.structure', ['items' => $items]);
    }
}

I'm pretty sure, I have some data in database. So, what's the problem there? I'm getting always empty array from database. Thank for help



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire