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

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 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序