Laravel雄辩地插入具有多个关系的数据 [英] Laravel eloquent insert data with multiple relationship
问题描述
我有3张桌子:
User
- id
- email
UserAccount
- id
- user_id
- account_id
Account
- id
- user_id
Verification
- id
- user_id
- guid
每当我尝试添加user
时,我都会尝试发布一个帖子,它将自动添加一个account
带有空字段,但其中包含user_id
的Verification
表以及user_id
创建Account
的同时,它还应该记录UserAccount
user_id
和account_id
,但是我使用多对多关系belongsToMany
和sync
结束了此错误.如何雄辩地添加acct_id
和user_id
?
I am trying to achieve a post whenever I try to add a user
, it will automatically add an account
with empty fields but with user_id
in it, Verification
table with user_id
also, at the same time once the Account
has been created it should also record UserAccount
user_id
and account_id
but I ended up this error using many to many relationship belongsToMany
and sync
. How do I add the acct_id
and user_id
with eloquent?
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'acct_id' cannot be null (SQL: insert into `user_accounts` (`acct_id`, `user_id`) values (?, 17))
这是我到目前为止尝试过的.
This is what I've tried so far.
Controller.php
Controller.php
$user = new User();
$user->name = "Not set";
$user->email = $email;
$user->password = NULL;
$user->save();
$accounts = new Account();
$accounts->email = $email;
$user->account()->save($accounts);
$userAccount = new UserAccount();
$userAccount->userAccount()->sync([
$user->id,
$accounts->id
]);
User.php
public function account()
{
return $this->hasMany(Account::class);
}
public function userAccount()
{
return $this->belongsToMany(User::class, UserAccount::class, 'user_id', 'id');
}
UserACcount.php
UserACcount.php
public function user()
{
return $this->hasMany(User::class, 'user_id', 'id');
}
public function account()
{
return $this->hasMany(Account::class, 'acct_id', 'id');
}
public function userAccount()
{
return $this->belongsToMany(Account::class, UserAccount::class, 'acct_id', 'user_id');
}
Verification.php
Verification.php
public function user()
{
return $this->belongsTo(User::class, 'user_id', 'id');
}
Account.php
Account.php
public function user()
{
return $this->hasMany(User::class);
}
public function userAccount()
{
return $this->belongsTo(UserAccount::class);
}
我尝试使用此功能,并且完全可以正常工作,但是可以肯定的是,这是雄辩的工作方式.
I tried using this functionality and completely works fine but pretty sure this is how it works with eloquent.
$userAcct = new UserAccount();
$userAcct->create([
'user_id' => $user->id,
'acct_id' => $accounts->id
]);
有什么想法吗?
我也确实发现了这个相关问题( Laravel hasManyThrough )
I also have did found this related problem (Laravel hasManyThrough)
推荐答案
首先,您应该从account
表中删除user_id
,因为链接两个表的user_account
已经对其进行了引用.此外,如果您想利用雄辩的约定来猜测表名和字段,则应确保表名为users
,accounts
,verifications
和account_user
.
First of all, you should remove user_id
from the account
table because it is already referenced by user_account
which links both tables. Moreover, if you wanna take advantages of Eloquent conventions which allow it to guess table names and fields, you should make sure your tables are named users
, accounts
, verifications
and account_user
.
User.php
public function accounts()
{
return $this->belongsToMany(Account::class);
}
Account.php
Account.php
public function users()
{
return $this->belongsToMany(User::class);
}
如果account_user
仅存在于链接2个表,则UserAccount
模型是无用的.
The UserAccount
model is useless if account_user
exists only to links 2 tables.
然后,您可以使用观察者来获取基于事件的方法:每当创建用户时=>做某事
Then, you may use an observer to get an event-based approach: whenever an user is created => do something
https://laravel.com/docs/5.8/eloquent#observers
<?php
namespace App\Observers;
use App\Account;
use App\Verification;
class UserObserver
{
/**
* Handle the User "created" event.
*
* @param \App\User $user
* @return void
*/
public function created(User $user)
{
(new Verification)->user()->associate($user);
$account = new Account;
$account->save();
$user->accounts()->attach([
$account->id
]);
}
}
这篇关于Laravel雄辩地插入具有多个关系的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!