title: Insert a new record if not exist and update if exist, laravel 5.4
body: <p>Is there any way to insert a new record if doesn't exist and update the record if exist? the following is the code i using.</p>
  
  <pre><code>public function store(Request $request)
  {
      $inputs = $request-&gt;all();
      $this-&gt;validate($request,[
              'course_code'=&gt;'required'
          ]);
      $batch = $request-&gt;batch;
      $students = User::select('student_id')-&gt;where('batch', $batch)-&gt;get();
  
      $course_codes = $inputs['course_code'];
      $data=[];
      foreach ($students as $student) {
          foreach ($course_codes as $course_code) {
          $data[]=[
              'batch' =&gt; $batch,
              'student_id' =&gt; $student-&gt;student_id,
              'semester' =&gt; $inputs['semester'],
              'course_code' =&gt; $course_code,
              "created_at" =&gt;  \Carbon\Carbon::now(), # \Datetime()
              "updated_at" =&gt; \Carbon\Carbon::now()  # \Datetime()
              ]; 
          }
      }    
  
      DB::table('assign_batches')-&gt;insert($data);
      return redirect('/admin/assign/batch/create')-&gt;with('message', 'A batch has been assigned to courses successfully!');
  }
  </code></pre>
  
  <p>Here is my output when I inserted same records again. 
  <a href="https://i.stack.imgur.com/sRBW0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sRBW0.png" alt="enter image description here"></a>
  But I want one Student Id may have many Course Code but can not be duplicate. So I want to check if student has same course or courses then skip or update it and insert new records.
  Please help me.
  Thanks.</p>