laravel中的多个复选框中的数据未存储在数据库中(Data is not getting stored in database for multiple check box in laravel)

编程入门 行业动态 更新时间:2024-10-24 15:23:04
laravel中的多个复选框中的数据未存储在数据库中(Data is not getting stored in database for multiple check box in laravel)

我创建了一个表单,其中列出了多个选择日期。 但那些日子并没有存储在数据库中。

这是我的控制器:

public function store(EventRequest $request) { $input = Request::all(); $input['days_of_week'] = Input::get('days_of_week'); Event::create($input); return redirect('event'); }

我的观点是:

@foreach($days as $day) <ul> <li> {!! Form::checkbox("days_of_week[]", $day, null) , $day !!} </li> </ul> @endforeach

当我返回Input::get('days_of_week'); 它显示我选择的复选框。

如何存储复选框值以及表单中存在的所有其他字段。

I have created a form, in which I have a list of days to be selected multiple. But those days are not getting stored in database.

here is my controller:

public function store(EventRequest $request) { $input = Request::all(); $input['days_of_week'] = Input::get('days_of_week'); Event::create($input); return redirect('event'); }

and my view is:

@foreach($days as $day) <ul> <li> {!! Form::checkbox("days_of_week[]", $day, null) , $day !!} </li> </ul> @endforeach

when I return Input::get('days_of_week'); it displays the check boxes I select.

How can I store checkbox value along with all the other fields present in form.

最满意答案

像Jerodev所说,你不能将数组存储到数据库中。 你可以做的是序列化数组并存储它。 然后在检索值时,可以对其进行反序列化。

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

你可以像这样回复它

$daysOfWeek = unserialize(Event::find(1)->days_of_week);

Like Jerodev said, you cannot store array to the database. What you can do is serialize the array and store it. Then when retrieving the value, you can unserialize it.

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

and you can retrive it like this

$daysOfWeek = unserialize(Event::find(1)->days_of_week);

更多推荐

本文发布于:2023-07-30 01:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1321398.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   数据库中   框中   复选   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!