title: Laravel: avoid duplicate entries in database
body: <p>I'm building a Laravel API. I have a models called Reservations. I want to avoid that a user creates two reservations for the same product and time period.</p>
  
  <p>I have the following:</p>
  
  <pre><code>$reservation =  Reservation::firstOrCreate([
     'listing_id'    =&gt;  $request-&gt;listing_id,
     'user_id_from'  =&gt;  $request-&gt;user_id_from,
     'start_date'    =&gt;  $request-&gt;start_date,
     'end_date'      =&gt;  $request-&gt;end_date,
  ]);
  return new ReservationResource($reservation);
  </code></pre>
  
  <p>Documentation says:</p>
  
  <blockquote>
    <p>The firstOrCreate method will attempt to locate a database record
    using the given column / value pairs</p>
  </blockquote>
  
  <p>The above code is working:</p>
  
  <ul>
  <li>it does add the item to the database if it does not exist yet (in
  this case, the reply to the REST API returns the new model instance
  which is correct.</li>
  <li>it does not add the item to the database if the combination equals <code>listing_id, user_id_from, start_date and end_date</code>.</li>
  </ul>
  
  <p>However, in the latter case (item already exists), it will also return in the REST API reply a reservation with the ID of the matched row.</p>
  
  <p>Example: in the below table, reservation with id 2 already exists: </p>
  
  <pre><code>id  listing_id  user_id_from    start_date  end_date
  ------------------------------------------------------  
  1      2            3               2019-09-12  2019-10-14  
  </code></pre>
  
  <p>Sending the below REST API request:</p>
  
  <pre><code>{
      "listing_id": 2,
      "user_id_from": 3,
      "start_date": "2019-09-12",
      "end_date": "2019-10-14",
  }
  </code></pre>
  
  <p>returns</p>
  
  <pre><code>"data": {
      "id": 1,
      "user_id_from": 3,
      "listing_id": 2,
      "price": 388,
      "start_date": "2019-09-12",
      "end_date": "2019-10-14",
   }
  </code></pre>
  
  <p>I would want to have a JSON reply stating that the item already exists. How can this be achieved?</p>