如何在编辑时使用laravel获取存储在数据库中的序列化数据?

时间:2015-12-28 06:16:17

标签: laravel-5 laravel-5.1 checkboxlist

我创建了一个表单,其中我创建了一周中的字段,我将其作为序列化存储在数据库中。当我尝试编辑表单(即尝试更改表单的日期数据)时,编辑的数据不会存储在数据库中,而是出现错误

  

preg_replace():参数不匹配,pattern是一个字符串,而replacement是一个数组

如何将值保存到数据库?

这是我的代码:

控制器:

public function store(EventRequest $request)
{
    $checkbox = Input::get('days_of_week');
    $input = Request::all();
    $input['days_of_week'] = serialize(Input::get('days_of_week'));
    Event::create($input);

    return redirect('event');
}

public function edit($id)
{
    // get the event
    $event = Event::findOrFail($id);
    $s = Category::all()->where('parent_id','=','0');
    $days = array(
        'Monday' => 'Monday',
        'Tuesday' => 'Tuesday',
        'Wednesday' => 'Wednesday',
        'Thursday' => 'Thursday',
        'Friday' => 'Friday',
        'Saturday' => 'Saturday',
        'Sunday' => 'Sunday',
    );
            $daysOfWeek = unserialize(Event::find($id)->days_of_week);
    // show the edit form and pass the event
    return view('event.edit')->with('event', $event)->with('s',$s)->with('days',$days)->with('daysOfWeek',$daysOfWeek);
}

    public function update(EventRequest $request, $id)
{
    $event = Event::findOrFail($id);
    $input['days_of_week'] = serialize(Input::get('days_of_week'));
    $event->update($request->all());
    return redirect('event');
}

Edit.blade.php:

    <div class="dropdown">
    <a href="#">
        <input type="text" name="fname" class="hida" placeholder="select number of days"/>
        <p class="multiSel"></p>
    </a>
    <div class="mutliSelect">
        @foreach($days as $day)
            <ul>
                <li>
                    {!! Form::checkbox("days_of_week[]", $day, null) , $day !!}
                </li>
            </ul>
        @endforeach
    </div>
</div>

1 个答案:

答案 0 :(得分:1)

尝试使用以下内容更新"Update Method"

public function update(EventRequest $request, $id)
{
    $event = Event::findOrFail($id);
    $input = Request::all();
    $input['days_of_week'] = serialize(Input::get('days_of_week'));
    $event->update($input);
    return redirect('event');
}