Laravel:在插入之前检查记录是否存在

时间:2017-12-08 08:36:29

标签: php laravel-5 error-handling

我有两张表: assignments 表格(assignment_id列,Staff Nameasset_iddepartment,{{1 }},date)和 status 表(列assetsasset_idtag_nameserial_numbermodel_number)。 资产表格列与分配表格相关联。我想在分配表中执行插入,但在插入之前,我想检查分配表中是否已存在 color &如果它是 asset_id ='有效',那么插入应该抛出错误/不应该发生。

AssignmentsController

status

class AssignmentController extends Controller { public function assignment(){ $assignments = Assignment::all(); return view('assignment/index', ['assignments' => $assignments]); } public function add(Request $request){ $this->validate($request, [ 'assignment_id' => '', 'staff_name' => 'required', 'asset_id' => 'required', 'department' => 'required', 'date' => 'required', 'status' => 'required' ]); $assignments = new Assignment; $assignments ->assignment_id = $request->input('assignment_id'); $assignments ->staff_id = $request->input('staff_id'); $assignments ->asset_id = $request->input('asset_id'); $assignments ->department_id = $request->input('department_id'); $assignments ->date = $request->input('date'); $assignments ->status = $request->input('status'); $count = Assignment::where('status', '=' ,'Active' )-> where('asset_id', '=' ,$assignments->asset_id)-> count(); if($count) { abort(405, 'Trying to assign an already assigned asset'); } $assignments ->save(); return redirect('/assignment/index') ->with('info', 'New Assignment Saved Successfully!'); }

Assginment.php

class Assignment extends Model { protected $primaryKey = 'assignment_id'; public function asset() { return $this->belongsTo('App\Asset', 'asset_id'); } } 中的以下变量和if语句始终会在 AssignmentsController 中引发错误,无论它是否为真且插入没有但是当整个语句被清除时,插入就会发生。

if statement

所以我想要一种方法来检查要插入的记录是否已经存在,条件好像存在 $count = Assignment::where('status', '=' ,'Active' )-> where('asset_id', '=' ,$assignments->asset_id)-> count(); if($count) { abort(405, 'Trying to assign an already assigned asset'); } ,然后检查 asset_id 如果它='活动'然后抛出错误但如果没有则插入发生。如果你理解,请帮忙。提前谢谢。

2 个答案:

答案 0 :(得分:0)

你可以这样做:

public function add(Request $request){
    $this->validate($request, [
        'assignment_id' => '',
        'staff_name' => 'required',
        'asset_id' => 'required',
        'department' => 'required',
        'date' => 'required',
        'status' => 'required'
    ]);
    $assignment = Assignment::firstOrNew([ 
           "status" => "Active", 
           "asset_id" => $assignments->asset_id
    ], $request->all()); 
    abort_if($assignment->exists(), 405, 'Trying to assign an already assigned asset'); 
    $assignment->save();
    return redirect('/assignment/index')
          ->with('info', 'New Assignment Saved Successfully!');
}

答案 1 :(得分:0)

解决方案

  $count = Assignment::where('asset_id', '=' ,$assignments->asset_id)->
        where('status', '=' ,'Active')->
        count();
        if($count == 1 && $assignments->status != 'Disable') {
            abort(405, 'Trying to assign an already assigned asset');
        }

我的代码中的错误:我正在计算所有分配,如果有任何活动状态我收到错误....我需要确切地检查我要分配的分配哪个是活动的或禁用然后分配应该发生与否的状态(活动/禁用)。谢谢你们所有人apokryfos