【问题标题】:Laravel - Dynamically Building Routes From ModelLaravel - 从模型动态构建路线
【发布时间】:2018-02-15 10:36:12
【问题描述】:

我正在尝试从下面的模型构建路线是代码。

$data = \App\Models\ModelName::all();

    if(!empty($data) && $data->count() >= 1)
    {
        foreach($data as $d)
        {
            Route::prefix('/'.$d['data_field'])->name($d['data_field'])->group(function(){
                Route::get('/', ucfirst($d['data_field']).'Controller@index');                  
            });
        }
    }

我不断收到 $d 未定义的错误消息。有没有办法动态构建路由?

【问题讨论】:

    标签: php mysql laravel-5 model


    【解决方案1】:

    您需要将$d 传递到闭包中,因为它不存在于匿名函数的范围内:

    Route::prefix('/'.$d['data_field'])
        ->name($d['data_field'])->group(function() use ($d) {
               Route::get('/', ucfirst($d['data_field']).'Controller@index');                  
        });
    

    【讨论】:

    • 非常感谢。完美答案
    猜你喜欢
    • 2014-06-17
    • 1970-01-01
    • 2019-06-09
    • 1970-01-01
    • 1970-01-01
    • 2020-12-20
    • 2016-07-20
    • 1970-01-01
    • 2019-03-04
    相关资源
    最近更新 更多