Laravel:在插入之前检查记录是否存在 [英] Laravel: Check if record exists Before Insertion
问题描述
我有两个表: assignments
表(列assignment_id
,Staff Name
,asset_id
,department
,date
,status
)和一个 assets
表(列asset_id
,tag_name
,serial_number
,model_number
,color
). 资产表列与分配表链接.我想在分配表中执行插入操作,但是在插入之前,我想检查 asset_id
是否已存在于分配表&中.如果它是 status
=有效",则插入应该引发错误/应该不会发生.
I have two tables: assignments
table(columns assignment_id
, Staff Name
, asset_id
, department
, date
, status
), and an assets
table (columns asset_id
, tag_name
, serial_number
, model_number
, color
). The asset table columns are linked with the assignments table. I want to perform an insertion into the assignments table but before the insertion, i want to check if the asset_id
already exists in the assignments table & if it's status
= 'Active' then the insertion should throw an error/shouldn't happen.
AssignmentsController
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
Assginment.php
class Assignment extends Model
{
protected $primaryKey = 'assignment_id';
public function asset()
{
return $this->belongsTo('App\Asset', 'asset_id');
}
}
AssignmentsController
中的以下变量和if语句始终会在 if statement
中引发错误,无论该错误与否以及插入不会发生,但是清除整个语句后,就会发生插入.
The below variable and if statement in the AssignmentsController
always throws the error in the if statement
whether it's true or not and the insertion doesn't happen but when the whole statement is cleared, the insertion happens.
$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
一样,然后检查是否为 status
='Active',然后抛出一个错误,但如果没有,则发生插入.如果您理解,请提供帮助.预先感谢.
So i want a way to check if the the record to be inserted already exists with conditions as if asset_id
exists then check for it's status
if it's ='Active' then throw an error but if not then insertion happens. Please help if you understand it. Thanks in advance.
推荐答案
解决方案
$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');
}
我的代码中的错误:我正在计算所有分配,并且如果有任何活动状态,我会得到一个错误....我需要准确检查要分配的分配是活动的还是禁用的,然后分配应该发生或与它的状态无关(活动/禁用).谢谢大家esp apokryfos .
The mistake in my code: I was counting all Assignments and if there is any active status i got an error....I needed to check exactly that assignment which i want to assign which was either active or disable then the assignment should occur or not pertaining it's status(active/disable). Thanks Y'all esp apokryfos.
这篇关于Laravel:在插入之前检查记录是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!