title: Laravel Cannot delete or update a parent row: a foreign key constraint fails
body: <p>For some reason a user cannot delete a post if it has been liked, it was working before but when I linked posts with likes I have been getting this error, I can't even delete it in Sequel Pro, unless I delete the likes associated with the post first.</p>
  <p><strong>Error</strong></p>
  <blockquote>
  <p>SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or
  update a parent row: a foreign key constraint fails
  (<code>eliapi8</code>.<code>likes</code>, CONSTRAINT <code>likes_post_id_foreign</code> FOREIGN KEY
  (<code>post_id</code>) REFERENCES <code>posts</code> (<code>id</code>)) (SQL: delete from <code>posts</code> where
  <code>id</code> = 149)</p>
  </blockquote>
  <p>Maybe it's my schema?</p>
  <p><strong>Posts Schema</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>Schema::create('posts', function (Blueprint $table) {
      $table-&gt;increments('id');
      $table-&gt;string('title');
      $table-&gt;text('body');
      $table-&gt;integer('user_id')-&gt;unsigned();
      $table-&gt;foreign('user_id')-&gt;references('id')-&gt;on('users');
      $table-&gt;timestamps();
  });
  </code></pre>
  <p><strong>Likes Schema</strong></p>
  <pre class="lang-php prettyprint-override"><code>Schema::create('likes', function (Blueprint $table) {
      $table-&gt;increments('id');
      $table-&gt;integer('post_id')-&gt;unsigned();
      $table-&gt;integer('user_id')-&gt;unsigned();
      $table-&gt;foreign('post_id')-&gt;references('id')-&gt;on('posts');
      $table-&gt;foreign('user_id')-&gt;references('id')-&gt;on('users');
      $table-&gt;softDeletes();
      $table-&gt;timestamps();
  });
  </code></pre>
  <p>I can like and unlike a post, but a user cannot delete a post that has been liked.</p>
  <p><strong>PostController.php</strong></p>
  <pre class="lang-php prettyprint-override"><code>public function destroy(Post $post){
      
      $this-&gt;authorize('delete', $post);
      $postl =  Post::with('likes')-&gt;whereId($post)-&gt;delete();
  
      if ($post-&gt;delete()) {
          if($postl){
               return response()-&gt;json(['message' =&gt; 'deleted']);
          }  
      };
  
      return response()-&gt;json(['error' =&gt; 'something went wrong'], 400);
  }
  </code></pre>