[英]Array to string conversion laravel
在我的刀片视图中,我有这样的输入:
<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 中做到这一点?
Controller (存储功能)
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'));
}
Controller (验证器功能)
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'
]);
}
Model
<?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'
];
}
$string='hello,min,max';
implode(',',$array);
在 model 中,我添加了此代码并且有效,我认为这很有帮助
protected $casts = [
'place_id' => 'integer',
'opening_hours' => 'array',
'kitchen_hours' => 'array',
'shipping_hours' => 'array',
'breakfast_hours' => 'array',
'launch_hours' => 'array',
'reservation_hours' => 'array'
];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.