drcb19700 2017-01-18 23:16
浏览 184
已采纳

Laravel在编辑视图中显示多选保存的值

I have a Project model which has a multi-select field called project_type. I am using Select2 for the field.

I've set up an enum in config/enum.php like this:

<?php

return [
    'project_types' => [
        '0' => 'WordPress Brochure Website',
        '1' => 'WordPress WooCommerce Website',
        '2' => 'Drupal Website',
        '3' => 'SEO',
        '4' => 'Branding',
        '5' => 'Bespoke Web App'
    ]
];

?>

In my create view I have a form with the field as such:

{{ Form::open(array('url' => 'projects')) }}

        <div class="form-group">
            {!! Form::label('Project Type') !!}
            {!! Form::select('project_type[]', Config::get('enums.project_types'), null, ['multiple'=>'multiple']); !!}
        </div>

{{ Form::submit('Create Project', array('class' => 'btn btn-primary')) }}

{{ Form::close() }}

Then in my controller when I save the multi-select field values I convert the array into a string to store into the DB:

public function store(ProjectRequest $request)
    {
        $project_type = $request->input('project_type');
        $project_type = implode(',', $project_type);
        $input = $request->except('project_type');
        $input['project_type'] = $project_type;
        Project::create($input);
        return redirect()->route('projects')->with('message', 'Rate created.');
    }

This will store the multi-select field in a format like this 0,1,3. This saves fine however now when I want to edit the project I need to pull out the values from the db and populate the multi-select field, this is where I'm struggling, here is what I have in my edit view.

   {{ Form::model($project, array('route' => array('projects.update', $project->id), 'method' => 'PUT')) }}

                <div class="form-group">
                    {!! Form::label('Project Type') !!}
                    {!! Form::select('project_type[]', Config::get('enums.project_types'), 1, ['multiple'=>'multiple']); !!}
                </div>

                {{ Form::submit('Update Project', array('class' => 'btn btn-primary')) }}

{{ Form::close() }}

At the moment you can see I've hard coded the default value for the multi-select field to 1 but is there a way I can set the default value of the multi-select field to what is stored in the db?

  • 写回答

1条回答 默认 最新

  • doudiandi6967 2017-01-18 23:35
    关注

    Basically exactly what @Jonathon said in the comments, I need to turn my imploded string back into an array and pass that to the default value parameter:

    <?php
        // explode the saved data back into an array                    
        $project_type = explode(',', $project->project_type);
    ?>
    
        <div class="form-group">
            {!! Form::label('Project Type') !!}
            {!! Form::select('project_type[]', Config::get('enums.project_types'), $project_type, ['multiple'=>'multiple']); !!}
        </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里