Laravel 模型由工厂创建的不包含数据库数据。

huangapple go评论76阅读模式
英文:

Laravel model created not by factory doesn't contain database data

问题

I am trying to create a model, save it in the database and attach relational data to it. To do this I don't want to use the database factory, because the database factory uses random values for attributes I didn't provide.

我正在尝试创建一个模型,将其保存到数据库并附加相关数据。为此,我不想使用数据库工厂,因为数据库工厂会为我未提供的属性使用随机值。

I tried the following:

我尝试了以下方法:

$user = User::create([
'user = User::create([
'first_name' => $args['first_name'],
'last_name' => $args['last_name'],
'email' => $args['email'],
'password' => \Illuminate\Support\Str::random(),
]);

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

dd($user->client);

This returns null for client, but when I do it like this:

这会返回null给client,但是当我这样做时:

$user = User::factory()->create([
'first_name' => $args['first_name'],
'last_name' => $args['last_name'],
'email' => $args['email'],
'password' => \Illuminate\Support\Str::random(),
]);

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

dd($user->client);

It does work. I can see in the Laravel Factory class that it uses the store() method which sets connections in the relations for the model. Could that be reason why the factory generated one does live updates and the manually creates one doesn't?

这确实有效。我可以在Laravel工厂类中看到它使用了store()方法,该方法设置了模型的关系连接。这是否是工厂生成的模型可以进行实时更新而手动创建的模型不能的原因?

Please let me know how I can generate a model like factory without using a factory!

请告诉我如何生成类似工厂的模型而不使用工厂!

英文:

I am trying to create a model, save it in the database and attach relational data to it. To do this I don't want to use the database factory, because the database factory uses random values for attributes I didn't provide.

I tried the following:

$user = User::create([
    'first_name' => $args['first_name'],
    'last_name' => $args['last_name'],
    'email' => $args['email'],
    'password' => \Illuminate\Support\Str::random(),
]);

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

dd($user->client);

This returns null for client, but when I do it like this:

$user = User::factory()->create([
    'first_name' => $args['first_name'],
    'last_name' => $args['last_name'],
    'email' => $args['email'],
    'password' => \Illuminate\Support\Str::random(),
]);

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

dd($user->client);

It does work. I can see in the Laravel Factory class that it uses the store() method which sets connections in the relations for the model. Could that be reason why the factory generated one does live updates and the manually creates one doesn't?

Please let me know how I can generate a model like factory without using a factory!

答案1

得分: 1

你在示例中没有展示$client,但是要像文档中那样在最后添加save()

$client = Client::find($id);

$user = User::create([
    'first_name' => $args['first_name'],
    'last_name' => $args['last_name'],
    'email' => $args['email'],
    'password' => \Illuminate\Support\Str::random(),
]);

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

$user->save();

dd($user->client);
英文:

You don't show your $client on the example. But add save() to the end like the docs.

$client = Client::find($id);

$user = User::create([
    'first_name' => $args['first_name'],
    'last_name' => $args['last_name'],
    'email' => $args['email'],
    'password' => \Illuminate\Support\Str::random(),
]);

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

$user->save();

dd($user->client);

答案2

得分: 0

首先,在associate之后,你需要添加save()方法:

$user->save();

在Laravel中,每个模型都有许多内置方法,如save、create、update和delete等:

DB::table('table_name')->insert([data]);
ModelName::create([data]);

$model = new ModelName();
$model->name = 'value';
$model->save();

这些方法中的每一个都依赖于模型内部的不同方法,或者依赖于模型之外的不同类,比如DB类。它们各自在后台扮演不同的角色。如果你使用观察者设计模式,你会发现其中一些会被观察并与观察者设计模式一起工作,而另一些则不会。

这就是导致你在dd结果中看到的明显差异的原因。这种明显差异出现在事件的分发中。

希望这能帮助你了解差异的根源,以避免将来出现类似的问题。

英文:

Briefly, here are some details

First you have to add save() after associate

$user->save();

Any model in Laravel has many methods inside it like save create update delete etc ...

DB::table('table_name')->insert([data])


ModelName::create([data]);



$model = new ModelName();
$model->name = 'value';
$model->save();

Each of them depends on a different mothod within the model or depends on a different class other than the model, such as the DB

And each methods of them plays a different role in the background. If you use the Observer Design Pattern, you will find that some of them are Observed and work with the Observer Design Pattern And some of them don't

This is the reason for the apparent difference in the event that appeared to you in the result of the dd

The apparent difference was in dispatches Events

I hope this helps you to know where the difference comes from to avoid such problems in the future

huangapple
  • 本文由 发表于 2023年6月15日 06:15:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76477916.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定