0

I have an assignments table (columns assignment_id, staff_name, asset_id, department, date, status) Currently my assignments table contains details of assets assigned to staff members and their departments.

In code I want to check if the asset_id already exists with it's status = 'Active' then insertion should throws an error but if the asset_id already exists with it's status = 'Disable' the insertion should proceed even if it's the asset_id exists.

The status columns with value as 'Disable' means the asset is not currently assigned to someone on the other hand, status = 'Active' means the asset is currently assigned to someone since it's assigned to someone you cannot assigned to someone else as 'Active' again but can assign as 'Disable'

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

Assuming assignment_id '4' is the last insertion, it should throw an error and not insert because you can't have one asset currently assigned to different people but if it were to be 'Disable' then insert.

Here is Controller as well:

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

The below variable and if statement in the AssignmentsController always throws the error in the if statement when the asset_id already exist instead of checking it's status also and the insertion doesn't happen.

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

Please help if you understand it. Thanks in advance.

11
  • So do you get any error when assigning the active assignment? Or it just saves it?
    – lewis4u
    Dec 10, 2017 at 21:30
  • it throws back the message in the abort function whether the assignment is active or disable. which should only be so when the for an active assignment of a particular asset that already exists
    – Nii
    Dec 10, 2017 at 21:37
  • Well that is your mistake in code, you are counting all Assignments and if there is any active you will get that error....you need to check exactly that assignment which you want to assign!
    – lewis4u
    Dec 10, 2017 at 21:39
  • Can you kindly help me out with an updated code of my Controller b'cos i don't know how to get it done. @lewis4u
    – Nii
    Dec 10, 2017 at 22:11
  • No, unfortunately I don't have time...You need to code and learn more. You can try to find help here: codereview.stackexchange.com
    – lewis4u
    Dec 10, 2017 at 22:14

2 Answers 2

1

The Solution

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

As suggested by IJas, The mistake in my code, I was counting all Assignments from my previous code and if there was any active status i got an error....I needed to check exactly that assignment which i wanted to assign whether it was active or disable then the assignment would occur or not pertaining it's status(active/disable). Thanks Y'all.

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

             
2
  • 1
    please explain, how and why, do not just post code, also format code proprly Nov 24, 2020 at 13:22
  • .. if you do... it prevents down-voting and wasiting time of all of us. Welcome and enjoy SO. End of Review.
    – ZF007
    Nov 24, 2020 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.