title: Laravel - SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
body: <p>I am building a project in Laravel on trying to persist a record in the database for the model Content that I have set up like this:</p>
  
  <pre><code>class Content extends Model
  {
      /**
       * The attributes that are guarded by mass assignable.
       *
       * @var array
       */
      protected $guarded = [
          'id'
      ];
  
      public function contentType()
      {
          return $this-&gt;hasOne('App\ContentType');
      }
  }
  </code></pre>
  
  <p>In my controller I am first saving the record in the content_types table like this:</p>
  
  <pre><code>    public function resolveType($type)
      {
          return ContentType::firstOrCreate(
            ['name' =&gt; $type,
            'slug' =&gt; str_slug($type, '-')]
          );
      }
  </code></pre>
  
  <p>And then I am saving the content:</p>
  
  <pre><code>        Content::create([
            'ct_id' =&gt; $post-&gt;ID,
            'cms_id' =&gt; '',
            'title' =&gt; $post-&gt;post_title,
            'slug' =&gt; str_slug($post-&gt;post_title, '-'),
            'excerpt' =&gt; $post-&gt;post_excerpt,
            'body' =&gt; $this-&gt;formatBodyImages($post-&gt;post_content),
            'password' =&gt; $post-&gt;post_password,
            'parent_id' =&gt; $post-&gt;post_parent,
          ]);
  </code></pre>
  
  <p>The ContentType model is set up like this:</p>
  
  <pre><code>class ContentType extends Model
  {
      /**
       * The attributes that are guarded by mass assignable.
       *
       * @var array
       */
      protected $guarded = [
          'id'
      ];
  
      public function contents()
      {
          return $this-&gt;hasMany('App\Content', 'ct_id');
      }
  }
  </code></pre>
  
  <p>So, when I try to persist new content record, I get the error:</p>
  
  <blockquote>
    <p>SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or
    update a child row: a foreign key constraint fails
    (<code>middleton</code>.<code>contents</code>, CONSTRAINT <code>contents_ct_id_foreign</code> FOREIGN
    KEY (<code>ct_id</code>) REFERENCES <code>content_types</code> (<code>id</code>)) (SQL: insert into
    <code>contents</code> (<code>cms_id</code>, <code>title</code>, <code>slug</code>, <code>excerpt</code>, <code>body</code>, <code>password</code>,
    <code>parent_id</code>, <code>updated_at</code>, <code>created_at</code>)</p>
  </blockquote>