简体   繁体   中英

Laravel: Check if record exists Before Insertion

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

class Assignment extends Model
{
    protected $primaryKey = 'assignment_id';    

    public function asset()
    {
        return $this->belongsTo('App\Asset', 'asset_id');
    }
}

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

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.

You can just do:

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

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM