title: Laravel - SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'posts_user_id_unique
body: <p><strong>I have an error</strong>:</p>
  <blockquote>
  <p>SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'posts_user_id_unique'</p>
  </blockquote>
  <p>If I create post and using the same user, the error will appear</p>
  <p>and if you use a different user, it will works.</p>
  <p>this my code in migration, <strong>create_posts_table.php</strong>:</p>
  <pre><code>$table-&gt;id();
  $table-&gt;foreignId('user_id')-&gt;unique()-&gt;constrained()-&gt;onDelete('cascade');
  $table-&gt;integer('category_id')-&gt;unsigned()-&gt;index();
  </code></pre>
  <p><strong>create_categories_tables.php:</strong></p>
  <pre><code>$table-&gt;id();
  $table-&gt;string('name')-&gt;constrained()-&gt;onDelete('cascade');
  </code></pre>
  <p>and this is my code in controller, <strong>PostController:</strong></p>
  <pre><code>public function create(){
          $this-&gt;authorize('create', Post::class);
          $categories = Category::pluck('name','id');
          return view ('admin.posts.create', ['categories'=&gt;$categories]);
      }
  public function store(){
          $this-&gt;authorize('create', Post::class);
          $inputs = request()-&gt;validate([
              'title'=&gt;'required',
              'post_image'=&gt;'file', //mime: jpeg, png
              'body'=&gt;'required',
              'category_id'=&gt; 'required'
          ]);
          if(request('post_image')){
              $inputs['post_image'] = request('post_image')-&gt;store('images');
          }
          auth()-&gt;user()-&gt;posts()-&gt;create($inputs);
          session()-&gt;flash('post-create-message', 'Post was Created ' . $inputs['title']);
          // return back();
          return redirect()-&gt;route('post.index');
  </code></pre>