Error saving Standard 5.8

Asked

Viewed 135 times

0

I have two related tables at the time of saving is accusing the following SQL error:

"SQLSTATE[HY000]: General error: 1364 Field 'id_data' doesn’t have a default value (SQL: Insert into users (email_user, password_user, updated_at, created_at) values (vfd, vgd, 2019-06-26 13:18:37, 2019-06-26 13:18:37))

I thought it was a problem a relationship problem, but without the relationship the mistake persists.

Model Users

use Illuminate\Database\Eloquent\Model;
use App\Models\Data_users;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Facades\App;

class Users extends Model
{
    protected $primaryKey = 'id'; 
    protected $fillable=[

                            'email_user',
                            'password_user',
                            'id_data'
                        ];

    public function userky(){
        return $this->belongsTo(Data_users::class);
    }
}

Model Data_users

use App\Models\Users;
use Illuminate\Database\Eloquent\Model;

class Data_users extends Model
{

    protected $fillable= [
                            'name_user',
                            'mobile_phone_user',
                            'rg_user',
                            'cpf_user',
                            'nationalite_user', 
                            'creci',
                            'state_civil_user',
                            'profition_user'
                        ];



}

Controller

 public function save_user(Request $request)
    {

      $datacad=$request->all();

      $data_user=Data_users::create($datacad);
      $user=Users::create($datacad);



      $user->data_user()->associate($data_user);
      $user->save();





      //  dd($request->all());




    }
  • 2

    Your table users has the column id_data, worthless default and you’re not setting a value for it in the insert.

  • Thanks Anderson Carlos Woss was that.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.