无法在Laravel的config文件夹中使用模型

问题描述 投票:0回答:1

我有一个使用PHP生成导航栏的插件。该文件夹位于project / config / menu.php中

它看起来像这样:

<?php

return [

//HORIZONTAL MENU LAYOUT -  MENU

    'horizontal' => [
        [
            'title' => 'bar',
            'link'  => '/bar/all',
            'active' => 'bar*',
            'icon'  => 'fa fa-sign-in',

        ],
        [
            'title' => 'foo',
            'link'  => '/foo/all',
            'active' => 'foo*',
            'icon'  => 'fa fa-sign-out',
        ],
    ]
];

我想添加一些模型信息。

这是我的尝试:

<?php
use Auth;
$id = Auth::user()->id;

return [

//HORIZONTAL MENU LAYOUT -  MENU

    'horizontal' => [
        [
            'title' => 'bar',
            'link'  => '/bar/'. $id,
            'active' => 'bar*',
            'icon'  => 'fa fa-sign-in',

        ],
        [
            'title' => 'foo',
            'link'  => '/foo/all',
            'active' => 'foo*',
            'icon'  => 'fa fa-sign-out',
        ],
    ]
];

我收到这个错误:Class 'Auth' not found。我也尝试过模特:

$model = \App\Model::count();

这给了我这个错误:

Call to a member function connection() on null

我如何在这里使用这些模型?

php laravel
1个回答
3
投票

laravel config在任何其他事情之前加载,因此实例化模型将给出错误,并且您获得的错误是由于在此特定配置文件加载期间没有加载数据库连接信息。我想知道为什么你需要在配置中调用模型,你可以简单地构建类似菜单布局的模板,如下所示:

<?php

return [

//HORIZONTAL MENU LAYOUT -  MENU

    horizontal' => [
        [
            'title' => 'bar',
            'link'  => '/bar/%d', // here %d is userId from database
            'active' => 'bar*',
            'icon'  => 'fa fa-sign-in',

        ],
        [
            'title' => 'foo',
            'link'  => '/foo/all',
            'active' => 'foo*',
            'icon'  => 'fa fa-sign-out',
        ],
    ]
];

然后用模型中的值替换%d

© www.soinside.com 2019 - 2024. All rights reserved.