【发布时间】:2020-06-24 12:36:04
【问题描述】:
在我的刀片视图中,我有这样的输入:
<input type="time" class="" name="shipping_hours[saturday][from]" value="" />
<input type="time" class="" name="shipping_hours[saturday][to]" value="" />
<input type="checkbox" name="shipping_hours[saturday][all_day]" /> <label class="">24h</label>
我在 mysql 数据库中有一个表,shipping_hours。我想以这种 JSON 格式保存数据:
{"monday":{"from":"13:00","to":"22:00"},"tuesday":{"from":"13:00","to":"22:00"},"wednesday":{"from":"13:00","to":"22:00"},"thursday":{"from":"13:00","to":"22:00"},"friday":{"from":"13:00","to":"01:00"},"saturday":{"from":"13:00","to":"01:00"},"sunday":{"from":"12:00","to":"22:00"}}
我如何在 laravel 中做到这一点?
控制器(存储功能)
public function store(Request $request)
{
$data = $this->validator($request->all())->validate();
settings_hours::create($data);
session()->flash('message_success', "Object added");
return redirect(route('hours'));
}
控制器(验证器功能)
protected function validator($data) {
return Validator::make($data, [
'place_id' => 'required',
'opening_hours.*' => 'nullable',
'kitchen_hours.*' => 'nullable',
'shipping_hours.*' => 'nullable',
'breakfast_hours.*' => 'nullable',
'launch_hours.*' => 'nullable',
'reservation_hours.*' => 'nullable'
]);
}
型号
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class settings_hours extends Model
{
public $timestamps = false;
protected $fillable = [
'place_id',
'opening_hours',
'kitchen_hours',
'shipping_hours',
'breakfast_hours',
'launch_hours',
'reservation_hours'
];
}
【问题讨论】:
-
欢迎。 SO问题需要某种格式才能适合我们的平台。你尝试了什么?你尝试的结果是什么?它与您的预期有何不同?我们是否产生了任何错误信息?请查看how to ask,然后查看
edit。如果没有此信息,该问题看起来就像是“给我代码”、免费编码服务问题或教程请求,这是不合法的。 SO 旨在处理特定编码问题、错误等。添加更多信息可以让我们的志愿者社区帮助您解决问题。