title: Laravel: Check if record exists Before Insertion
body: <p>I have two tables: <strong><code>assignments</code></strong> table(columns <code>assignment_id</code>, <code>Staff Name</code>, <code>asset_id</code>, <code>department</code>, <code>date</code>, <code>status</code>), and an <strong><code>assets</code></strong> table (columns <code>asset_id</code>, <code>tag_name</code>, <code>serial_number</code>, <code>model_number</code>, <code>color</code>). The <strong>asset</strong> table columns are linked with the <strong>assignments</strong> table. I want to perform an insertion into the assignments table but before the insertion, i want to check if the <strong><code>asset_id</code></strong> already exists in the assignments table &amp; if it's <strong><code>status</code></strong> = 'Active' then the insertion should throw an error/shouldn't happen.</p>
  
  <p><strong>AssignmentsController</strong></p>
  
  <pre><code>class AssignmentController extends Controller
  {
  
  public function assignment(){
      $assignments = Assignment::all();
      return view('assignment/index', ['assignments' =&gt; $assignments]);
  }   
  
    public function add(Request $request){
  
      $this-&gt;validate($request, [
          'assignment_id' =&gt; '',
          'staff_name' =&gt; 'required',
          'asset_id' =&gt; 'required',
          'department' =&gt; 'required',
          'date' =&gt; 'required',
          'status' =&gt; 'required'
      ]);
      $assignments =  new Assignment;
      $assignments  -&gt;assignment_id = $request-&gt;input('assignment_id');
      $assignments  -&gt;staff_id  = $request-&gt;input('staff_id');
      $assignments  -&gt;asset_id = $request-&gt;input('asset_id');
      $assignments  -&gt;department_id = $request-&gt;input('department_id');
      $assignments  -&gt;date  = $request-&gt;input('date');
      $assignments  -&gt;status = $request-&gt;input('status');
  
  $count = Assignment::where('status', '=' ,'Active' )-&gt;
      where('asset_id', '=' ,$assignments-&gt;asset_id)-&gt;
      count();
      if($count) {
          abort(405, 'Trying to assign an already assigned asset');
      }
  
      $assignments  -&gt;save();
      return redirect('/assignment/index') -&gt;with('info', 'New Assignment Saved Successfully!');
  }
  </code></pre>
  
  <p><strong><code>Assginment.php</code></strong></p>
  
  <pre><code>class Assignment extends Model
  {
      protected $primaryKey = 'assignment_id';    
  
      public function asset()
      {
          return $this-&gt;belongsTo('App\Asset', 'asset_id');
      }
  }
  </code></pre>
  
  <p>The below variable and if statement in the <strong><code>AssignmentsController</code></strong> always throws the error in the <strong><code>if statement</code></strong> whether it's true or not and the insertion doesn't happen but when the whole statement is cleared, the insertion happens. </p>
  
  <pre><code>$count = Assignment::where('status', '=' ,'Active' )-&gt;
          where('asset_id', '=' ,$assignments-&gt;asset_id)-&gt;
          count();
          if($count) {
              abort(405, 'Trying to assign an already assigned asset');
          } 
  </code></pre>
  
  <p>So i want a way to check if the the record to be inserted already exists  with conditions as if <strong><code>asset_id</code></strong> exists then check for it's <strong><code>status</code></strong> if it's ='Active' then throw an error but if not then insertion happens. Please help if you understand it. Thanks in advance.</p>