简体   繁体   English

在具有关系的多个表中创建新记录

[英]Create a new record in multiple tables with relationsship

I croak that there is something that escapes me.我发牢骚说有什么东西让我逃脱了。

I have a User model that has a hasMany relationship with another model called Domain我有一个用户 model 与另一个名为 Domain 的 model

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我有多个连接,并且在用户 model 中使用 trais HasApiTokens

After see this, I think the problem is other question., I apologize for inconvenients.看到这个后,我认为问题是其他问题。对于给您带来的不便,我深表歉意。

You have to wait for the user to be created, and then adding its id to the domains table您必须等待创建用户,然后将其 id 添加到域表中

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();

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 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM