Laravel 使用默认数据自动创建关系条目
Laravel auto create relation entry with default data
我正在尝试在我的 Laravel 应用程序中制作一个钱包系统。
我的数据库有默认的 users
table 并且每个用户有一个 wallet
.
像这样:
// User model
public function wallet()
{
return $this->hasOne(Wallet::class);
}
每当我查询 wallet
时,我都希望像:
$user->wallet
如果用户没有wallet
,我想使用默认值自动创建一个新的wallet
。
如何有条理、高效地做到这一点?
我不知道,但你应该看看这个
//copy attributes from original model
$newRecord = $original->replicate();
// Reset any fields needed to connect to another parent, etc
$newRecord->some_id = $otherParent->id;
//save model before you recreate relations (so it has an id)
$newRecord->push();
//reset relations on EXISTING MODEL (this way you can control which ones will be loaded
$original->relations = [];
//load relations on EXISTING MODEL
$original->load('somerelationship', 'anotherrelationship');
//re-sync the child relationships
$relations = $original->getRelations();
foreach ($relations as $relation) {
foreach ($relation as $relationRecord) {
$newRelationship = $relationRecord->replicate();
$newRelationship->some_parent_id = $newRecord->id;
$newRelationship->push();
}
}
通过“我希望如果用户没有钱包,应该使用默认值自动创建一个新钱包”如果你的意思是你在前端使用 $user->wallet
时想要一些默认值以避免条件,您可以使用方便的 withDefault()
,如下所示:
/**
* Get the wallet for the user.
*/
public function wallet()
{
return $this->hasOne(Wallet::class)->withDefault([
//Default values here
//This will not persist or create a record in database
// It will just return an instance of Wallet with default values defined here instead of null when a user doesn't have a Wallet yet
// It will help avoid conditionals say in frontend view
]);
}
您可以在 DefaultModel - Lavavel Docs
阅读更多内容
但是,如果您希望每个用户都有一个默认钱包,您可以使用模型挂钩并在创建记录时为每个用户创建一个钱包。
class User extends Model
{
public static function booted()
{
static::created(function($user){
$user->wallet()->create([
//Default values here...
]);
});
}
// Rest of the User Model class code...
}
阅读更多内容
选项 1:您可以使用 Default Models
public function wallet()
{
return $this->hasOne(Wallet::class)->withDefault([
'name' => 'Wallet Name',
]);
}
选项 2:在用户创建时插入 Model Events
protected static function boot()
{
parent::boot();
static::created(static function (self $user) {
$wallet = new Wallet(['name' => 'Wallet Name']);
$user->wallet()->save($wallet);
});
}
我正在尝试在我的 Laravel 应用程序中制作一个钱包系统。
我的数据库有默认的 users
table 并且每个用户有一个 wallet
.
像这样:
// User model
public function wallet()
{
return $this->hasOne(Wallet::class);
}
每当我查询 wallet
时,我都希望像:
$user->wallet
如果用户没有wallet
,我想使用默认值自动创建一个新的wallet
。
如何有条理、高效地做到这一点?
我不知道,但你应该看看这个
//copy attributes from original model
$newRecord = $original->replicate();
// Reset any fields needed to connect to another parent, etc
$newRecord->some_id = $otherParent->id;
//save model before you recreate relations (so it has an id)
$newRecord->push();
//reset relations on EXISTING MODEL (this way you can control which ones will be loaded
$original->relations = [];
//load relations on EXISTING MODEL
$original->load('somerelationship', 'anotherrelationship');
//re-sync the child relationships
$relations = $original->getRelations();
foreach ($relations as $relation) {
foreach ($relation as $relationRecord) {
$newRelationship = $relationRecord->replicate();
$newRelationship->some_parent_id = $newRecord->id;
$newRelationship->push();
}
}
通过“我希望如果用户没有钱包,应该使用默认值自动创建一个新钱包”如果你的意思是你在前端使用 $user->wallet
时想要一些默认值以避免条件,您可以使用方便的 withDefault()
,如下所示:
/**
* Get the wallet for the user.
*/
public function wallet()
{
return $this->hasOne(Wallet::class)->withDefault([
//Default values here
//This will not persist or create a record in database
// It will just return an instance of Wallet with default values defined here instead of null when a user doesn't have a Wallet yet
// It will help avoid conditionals say in frontend view
]);
}
您可以在 DefaultModel - Lavavel Docs
阅读更多内容但是,如果您希望每个用户都有一个默认钱包,您可以使用模型挂钩并在创建记录时为每个用户创建一个钱包。
class User extends Model
{
public static function booted()
{
static::created(function($user){
$user->wallet()->create([
//Default values here...
]);
});
}
// Rest of the User Model class code...
}
阅读更多内容
选项 1:您可以使用 Default Models
public function wallet()
{
return $this->hasOne(Wallet::class)->withDefault([
'name' => 'Wallet Name',
]);
}
选项 2:在用户创建时插入 Model Events
protected static function boot()
{
parent::boot();
static::created(static function (self $user) {
$wallet = new Wallet(['name' => 'Wallet Name']);
$user->wallet()->save($wallet);
});
}