【发布时间】:2019-06-17 15:41:11
【问题描述】:
如何插入不重复的值?我正在开发一个计时系统,现在的问题是当我添加时间并再次添加时间时,即时间重复的同一天。如何插入即使再次添加时间也不重复的时间?非常感谢这里是我的代码。有可能吗?
我当前的数据是这样的,我的最终输出一定是employee_no 10310必须只有一个,不能重复数据。它不应该重复,也不应该插入id 3。
控制器
public function insertSchedule(Request $request)
{
$employeeTimeSet = new Schedule;
$employeeTimeSet->employee_no = $request->input('hidEmployeeno');
$employeeTimeSet->last_name = $request->input('hidEmployeeLast');
$employeeTimeSet->first_name = $request->input('hidEmployeeFirst');
$employeeTimeSet->date_today = $request->input('dateToday');
$employeeTimeSet->time_in = $request->input('timeIn');
$employeeTimeSet->time_out = $request->input('timeOut');
$employeeTimeSet->save();
$notification = array(
'message' => 'Employee Time Set!',
'alert-type' => 'success'
);
return redirect('/admin/employeemaintenance/createSchedule')->with($notification, 'Employee Time Set');
}
查看
{!! Form::open(['action' => 'Admin\EmployeeFilemController@insertSchedule', 'method' => 'POST']) !!}
<div class="row">
<div class="form-group col-md-12">
<small>Employee No. and Name:</small>
<b><i> {{ $employee->employee_no }} : {{ $employee->last_name }}, {{ $employee->first_name }}</i></b>
<input type="hidden" name="hidEmployeeno" value='<?php echo $employee->employee_no ?>'>
<input type="hidden" name="hidEmployeeLast" value='<?php echo $employee->last_name ?>'>
<input type="hidden" name="hidEmployeeFirst" value='<?php echo $employee->first_name ?>'>
<hr>
</div>
</div>
<table class="table">
<thead>
<tr>
<th>DATE TODAY</th>
<th>TIME IN</th>
<th>TIME OUT</th>
<th>ACTION</th>
</tr>
</thead>
<tbody>
<tr>
<td><b><?php echo date("F-d-Y"); ?></b></td>
<!---Date Hidden-->
<input type="hidden" name="dateToday" value='<?php echo date("F-d-Y") ?>'>
<td><input type="time" name="timeIn" class="form-control col-md-10"></td>
<td><input type="time" name="timeOut" class="form-control col-md-10"></td>
<td> {{Form::button('<i class="fa fa-clock"> SET TIME</i>',['type' => 'submit','class' => 'btn btn-warning btn-sm', 'style'=>"display: inline-block;"])}}</td>
</tr>
</tbody>
</table>
{!! Form::close() !!}
数据库表
public function up()
{
Schema::create('schedules', function (Blueprint $table) {
$table->increments('id');
$table->string('employee_no')->nullable();
$table->string('last_name')->nullable();
$table->string('first_name')->nullable();
$table->string('date_today')->nullable();
$table->string('time_in')->nullable();
$table->string('time_out')->nullable();
$table->timestamps();
});
}
【问题讨论】:
-
你能用当前插入的值和你的期望值更新问题吗?
-
@Eisenheim,先生,我已经编辑了我的问题,您可以看看吗?谢谢
-
什么组合需要是唯一的:
employee_no+date_today?或者还有time_in/time_out? -
@HCK 是的,先生,您说得对! ,我还需要 time_in 和 time_out 因为当用户在其中设置时间时也不能是双倍的,你是对的,先生。
-
第 1 行和第 3 行的“时间”不同。您需要保留哪一个?第 3 行的数据时间是否错误?
标签: sql laravel laravel-5 eloquent laravel-blade