【问题标题】:How to Insert Value That is Not Repeating in Laravel如何在 Laravel 中插入不重复的值
【发布时间】: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">&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();
    });
}

【问题讨论】:

  • 你能用当前插入的值和你的期望值更新问题吗?
  • @Eisenheim,先生,我已经编辑了我的问题,您可以看看吗?谢谢
  • 什么组合需要是唯一的:employee_no+ date_today?或者还有time_in/time_out?
  • @HCK 是的,先生,您说得对! ,我还需要 time_in 和 time_out 因为当用户在其中设置时间时也不能是双倍的,你是对的,先生。
  • 第 1 行和第 3 行的“时间”不同。您需要保留哪一个?第 3 行的数据时间是否错误?

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


【解决方案1】:

在您的迁移中,更新此内容

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

到这里:

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

【讨论】:

  • 如果管理员在第二天输入时间怎么办?那么empoyee_no就不会再输入了?
  • My current data is this, and my final output must be that employee_no 10310 must only be one and not repeating data. It should not repeat and not insert id 3.。我以为您希望阻止类似的employee_no。 10310 将永远不会再次插入。但其他employee_no 会
  • 哦,先生,它一定让您感到困惑,对此感到抱歉.. 我的意思是,这一天每天都会自动更新,因此管理员需要每天输入时间。这就是我的意思,也适用于只需要一个值而不是重复的员工:)
【解决方案2】:

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

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');

}

【讨论】:

  • 先生,我不太清楚为什么它不起作用,但我们能否请您检查我们的代码和我的代码?非常感谢先生。
【解决方案3】:

您可以使用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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-03
    • 2021-06-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2019-10-20
    • 2019-11-17
    相关资源
    最近更新 更多