我有一个作业表(列assignment_id,staff_name,asset_id,部门,日期,状态) 目前,我的分配表包含分配给员工及其部门的资产的详细信息。
在代码中,我想检查asset_id是否已经存在,状态='有效'然后插入应该抛出错误,但如果asset_id已经存在,则状态='禁用'即使资产数据存在,插入也应继续进行。
状态列的值为'禁用'表示资产目前尚未分配给某人,状态='有效'表示该资产目前已分配给某人,因为该资产已分配给您无法分配给其他人的人,因为该资产是“有效的”。再次但可以指定为'禁用'
For Instance
Assignment_id Staff Asset_id Department Status Date
1 Reynold AIO002 Logistics Disable 12.03.2014
2 Kyle AIO002 Business Dev Disable 12.04.2014
3 Trisha AIO002 Revenue Active 25.06.2015
4 Mary AIO002 Waybill Active 21.09.2015
假设assignment_id' 4'是最后一次插入,它应该抛出一个错误而不是插入,因为你不能将一个资产当前分配给不同的人但是如果它是“禁用”#39;然后插入。
这也是Controller:
AssignmentsController
class AssignmentController extends Controller
{
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_name');
$assignments ->asset_id = $request->input('asset_id');
$assignments ->department_id = $request->input('department');
$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!');
}
当asset_id已经存在时,AssignmentsController中的下面的变量和if语句总是在if语句中抛出错误而不是检查它的状态,并且插入没有发生。
$count = Assignment::where('status', '=' ,'Active' )->
where('asset_id', '=' ,$assignments->asset_id)->
count();
if($count) {
abort(405, 'Trying to assign an already assigned asset');
}
如果您理解,请提供帮助。提前谢谢。
答案 0 :(得分: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');
}
根据IJas的建议,我的代码中的错误,我正在计算我之前代码中的所有分配,如果有任何活动状态我得到错误....我需要确切地检查哪个分配我想分配它是激活还是禁用,然后分配将发生或不与其状态(激活/禁用)相关。谢谢你们。
答案 1 :(得分:0)
$invoice_details = new invoiceDetail();
$invoice_details->date = date('Y-m-d',strtotime($request->date)); $invoice_details->invoice_id = $invoice->id;
$invoice_details->deptName_id = $request->deptName_id[$i];
$invoice_details->serialNo_id = $request->serialNo_id[$i];
$invoice_details->name = $request->name[$i];
$invoice_details->Status = '0';
$count = invoiceDetail::where('status', '=' ,'0' )->where('serialNo_id', '=' ,$invoice_details->serialNo_id)->count();
if($count)
{
return redirect()->back()->with('error', 'Sorry! You have been trying to insert double data');
}