我是 Laravel 的初学者。我在我的项目 Laravel 6 中使用。我有 2 个输入:<input maxlength="5" type="text" name="hours_worked" class="form-control edited valid" value="" required="" onkeypress="return isNumberKey(event)"><input type="text" name="minutes_worked" class="form-control edited valid" value="" required="" onkeypress="return isNumberKey(event)">我用以下代码保存到数据库:WorkingTime::create( $this->request->all()例如,当有人在字段中输入时,我会遇到问题。“90”分钟。我在数据库中保存了 90 分钟 - 我想节省 1 小时 30 分钟。我怎样才能用我的代码实现这个目标?我的架构:Schema::create('working_times', function (Blueprint $table) { $table->increments('id'); $table->integer('client_id')->unsigned(); $table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade'); $table->integer('case_id')->unsigned(); $table->foreign('case_id')->references('id')->on('case_instances')->onDelete('cascade'); $table->integer('user_id')->unsigned(); $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); $table->smallInteger('hours_worked'); $table->smallInteger('minutes_worked'); $table->string('name'); $table->string('appearance')->nullable(); $table->longText('description')->nullable(); $table->softDeletes(); $table->timestamps(); });
1 回答
MYYA
TA贡献1868条经验 获得超4个赞
这是一个粗略的解决方案,但不是这样做:
WorkingTime::create($this->request->all());
做:
$data = $this->request->all();
$hours = (int) $data['hours_worked'];
$minutes = (int) $data['minutes_worked'];
$overMinutes = $minutes % 60;
$hours += (int) (($minutes - $overMinutes) / 60);
$minutes = $overMinutes;
$data['hours_worked'] = $hours;
$data['minutes_worked'] = $minutes;
WorkingTime::create($data);
- 1 回答
- 0 关注
- 124 浏览
添加回答
举报
0/150
提交
取消