Integrity constraint violation: 1452 Cannot add or update a child row
<p>i got this error :</p>

<p>SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (<code>pumma2</code>.<code>users</code>, CONSTRAINT <code>users_roles_id_foreign</code> FOREIGN KEY (<code>roles_id</code>) REFERENCES <code>users</code> (<code>id</code>) ON DELETE CASCADE ON UPDATE CASCADE) (SQL: insert into <code>users</code> (<code>username</code>, <code>email</code>, <code>name</code>, <code>password</code>, <code>cover</code>, <code>roles_id</code>, <code>updated_at</code>, <code>created_at</code>) values (qweqwe, haikalhikmi@gmail.com, qweqweqew, $2y$10$YX2OPPTGEDcquYkgk.ln6eEuwtFqrtmRbIpvgEZqJJHR9gk8mfdfm, wallpaper-27058.jpg, 1, 2016-12-11 06:09:41, 2016-12-11 06:09:41))</p>

<p>this is my RegisterController</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>$user=$request-&gt;file('cover');
        $destination ='img/user';
        $filename=$user-&gt;getClientOriginalName();
        storage::put('img/user/'.$filename,file_get_contents($request-&gt;file('cover')-&gt;getRealPath()));
 
        $user = new User();
                
        $user-&gt;username = $request-&gt;username;
        $user-&gt;email = $request-&gt;email;
        $user-&gt;name = $request-&gt;name;
        $user-&gt;password = bcrypt($request-&gt;password);
        $user-&gt;cover = $filename;
        $user-&gt;roles_id = DB::table('roles')-&gt;select('id')-&gt;where('rolename','user')-&gt;first()-&gt;id;       
                 
        $user-&gt;save();</code></pre>
</div>
</div>
</p>

<p>when i try dd($user):</p>

<p>it works :</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>attributes: array:6 [▼
    "username" =&gt; "qweqwe"
    "email" =&gt; "haikalhikmi@gmail.com"
    "name" =&gt; "qweqweqew"
    "password" =&gt; "$2y$10$Ci5usZFP6ANCyBfQhN3Gzexh98Wb8R9n4AmVEvq8x/4bakfGF8l/y"
    "cover" =&gt; "wallpaper-27058.jpg"
    "roles_id" =&gt; 1
  ]</code></pre>
</div>
</div>
</p>

<p>just in case if you want to see my user table</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table-&gt;increments('id');
            $table-&gt;unsignedInteger('roles_id')-&gt;nullable();
            $table-&gt;string('username')-&gt;unique();
            $table-&gt;string('email')-&gt;unique();
            $table-&gt;string('name')-&gt;unique();
            $table-&gt;string('password');
            $table-&gt;string('cover');
            $table-&gt;rememberToken();
            $table-&gt;timestamps();
        });

        Schema::create('roles', function (Blueprint $table){
            $table-&gt;increments('id');
            $table-&gt;string('rolename');
        });

        schema::table('users', function (Blueprint $table){            
            $table-&gt;foreign('roles_id')-&gt;references('id')-&gt;on('users')-&gt;onDelete('cascade')-&gt;onUpdate('cascade');
            
        });

    }</code></pre>
</div>
</div>
</p>