0

I croak that there is something that escapes me.

I have a User model that has a hasMany relationship with another model called Domain

I thought it is possible to save the data in both tables in a single command but I see that it is not or not.

Tinker

use App\Models\Albarid\Domain;
$domain = new Domain;
use App\Models\Albarid\User;
$user = new User;
$user->name = 'Jhon Doe';
$user->email = 'mailo@ggmaiol.com'
$user->password = 'hajsdkjhaksdjkhas';
$user->c_password = 'hajsdkjhaksdjkhas';
$domain->domain = 'newdomain.com';

$user->domains()->save($domain)

Illuminate/Database/QueryException with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null (SQL: insert into `domains` (`domain`, `user_id`, `updated_at`, `created_at`) values (nuewdomain.com, ?, 2019-09-30 11:31:47, 2019-09-30 11:31:47))'

Is possible one way for save data in two tables in one command or need create first user?

but no.

I write the most widespread code since the problem of connections does not seem to be.

>>> use App\Models\Albarid\Domain;
>>> $col = Schema::getColumnListing('domain')
=> []
>>> $domain = new Domain;
=> App\Models\Albarid\Domain {#3140}
>>> use App\Models\Albarid\Domain;
>>> $domain = new Domain;
=> App\Models\Albarid\Domain {#3136}
>>> $domain->getTableColumns()
=> [
     "id",
     "user_id",
     "domain",
     "description",
     "disclaimer",
     "aliases",
     "mailboxes",
     "maillists",
     "maxquota",
     "quota",
     "transport",
     "backupmx",
     "settings",
     "created_at",
     "updated_at",
     "deleted_at",
     "full_deleted_at",
   ]
>>> $user->getTableColumns()
=> [
     "id",
     "name",
     "email",
     "email_verified_at",
     "password",
     "remember_token",
     "is_super_admin",
     "created_at",
     "updated_at",
   ]

Relationship exists

public function domains()
{
    return $this->hasMany('App\Models\Albarid\Domain');
}

There're a piece of problem.

I've multiple conecctions and also in User model use trais HasApiTokens

After see this, I think the problem is other question., I apologize for inconvenients.

abkrim
  • 3,512
  • 7
  • 43
  • 69

2 Answers2

0

You have to wait for the user to be created, and then adding its id to the domains table

use App\Models\Albarid\Domain;
$domain = new Domain;
use App\Models\Albarid\User;
$user = new User;
$user->name = 'Jhon Doe';
$user->email = 'mailo@ggmaiol.com'
$user->password = 'hajsdkjhaksdjkhas';
$user->c_password = 'hajsdkjhaksdjkhas';
$user->save();
$domain->domain = 'newdomain.com';
$domain->user_id = $user->id;
$domain->save();
mustafaj
  • 305
  • 1
  • 12
  • {"success":false,"message":"Problem when saving on database","data":["Trying to get property 'id' of non-object",64]} 64 is $user->id – abkrim Sep 30 '19 at 12:20
  • What fields does your user table have? – mustafaj Sep 30 '19 at 12:49
  • A lot of thanks. I've forget that i work with 5 database connecions and there're two tables named `domain` in differents connecionts. Tahnks – abkrim Sep 30 '19 at 14:45
0

First you need to define relationship and then you can use given link code to add data in multiple tables

https://laravel.com/docs/5.8/eloquent-relationships#updating-belongs-to-relationships

Example code

$account = App\Account::find(10);

$user->account()->associate($account);

$user->save();
Manoj Patel
  • 329
  • 3
  • 11