如何插入在Laravel中不重复的值

时间:2019-01-24 00:11:50

标签: sql laravel laravel-5 eloquent laravel-blade

如何插入不重复的值?我正在使用计时系统,现在的问题是当我添加时间并再次添加时间时(即重复时间的同一天)。如何添加即使再次添加时间也不会重复的时间?非常感谢,这是我的代码。可以这样做吗?

我当前的数据是这个,而我的最终输出必须是employee_no 10310必须是一个并且不能重复数据。它不应重复,也不能插入id 3

enter image description here

控制器

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">&nbsp;&nbsp;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();
    });
}

3 个答案:

答案 0 :(得分:0)

在您的迁移中,对此进行更新

$table->string('employee_no')->nullable();

对此:

$table->string('employee_no')->unique();

答案 1 :(得分:0)

我非常确定您可以在网站的其他方面进行改进,但是鉴于我们不知道您的用例,这应该可以解决问题:

public function insertSchedule(Request $request)
{
    $schedule = Schedule::where('employee_no', $request->input('hidEmployeeno'))
                ->where('date_today', $request->input('dateToday'))
                ->where('time_in', $request->input('timeIn'))
                ->where('time_out', $request->input('timeOut'))
                ->first();

    if( ! $schedule)
    {
        $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'
        );
    }
    else
    {
        $notification = array(
            'message' => 'The entry already exists!',
            'alert-type' => 'warning'
        );
    }


return redirect('/admin/employeemaintenance/createSchedule')
           ->with($notification, 'Employee Time Set');

}

答案 2 :(得分:0)

您可以使用firstOrNew()

$employeeTimeSet = Schedule::firstOrNew(['employee_no' => $request->input('hidEmployeeno'), 'date_today' => $request->input('dateToday')]);
$employeeTimeSet->last_name = $request->input('hidEmployeeLast');
$employeeTimeSet->first_name = $request->input('hidEmployeeFirst');
$employeeTimeSet->time_in = $request->input('timeIn');
$employeeTimeSet->time_out = $request->input('timeOut');
$employeeTimeSet->save();