douxidang9092 2016-11-29 06:26
浏览 16
已采纳

Twig也显示键而不是仅显示值

I am using Laravel 5.3 along with Twig templating engine (Not Blade). I am trying to generate a list of drop-down list in the form. Instead of showing just the values. It is showing the keys as well.

For example:

{"display_name":"Admin"}

In my view the code is

{{ form_select('roles', roles, input_get('role'), {id:'user_type',class:'form-control'}) }}

In the controller

public function create()
{
    $roles = Role::all('display_name');
    return view('users.user-add')->with(['roles' => $roles]);
}

What am I missing?

  • 写回答

1条回答 默认 最新

  • dongtui0650 2016-11-30 05:41
    关注

    I would suggest you to create a helper for this so that there is no requirement for you to pass roles from every controller to every view. Create a helper function like this :

    if(!function_exists('getRoles'))
    {
    function getRoles()
    {
        $roles = Role::get();
        foreach ($roles as $role)
        {
            $results[$role->id] = $role->name;
        }
    
        return $results;
    }
    }
    

    and now in your template do :

    {{ form_select('roles', getRoles(), input_get('role'), {id:'user_type',class:'form-control'}) }}
    

    I hope I understood your question as intended and this helps you achieve what you are looking for.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀