如何在编辑时使用 laravel 获取存储在数据库中的序列化数据?
How to fetch serialized data stored in database using laravel while edit?
我制作了表格,其中我为一周中的几天创建了字段,我将其存储在数据库中作为序列化。当我尝试编辑表单(即尝试更改表单的天数数据)时,编辑的数据不会存储在数据库中,而是出现错误
preg_replace(): Parameter mismatch, pattern is a string while replacement is an array
如何将值保存到数据库?
这是我的代码:
控制器:
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>
尝试用以下
更新您的 "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');
}
我制作了表格,其中我为一周中的几天创建了字段,我将其存储在数据库中作为序列化。当我尝试编辑表单(即尝试更改表单的天数数据)时,编辑的数据不会存储在数据库中,而是出现错误
preg_replace(): Parameter mismatch, pattern is a string while replacement is an array
如何将值保存到数据库?
这是我的代码:
控制器:
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>
尝试用以下
更新您的"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');
}