Laravel-最佳实践更新,并使用对象数组将记录保存到数据库 [英] Laravel - Best Practice Update and Save Record to database using array of object
问题描述
我想问一问最佳实践,当使用对象数组将数据保存和更新到数据库时,你们该怎么做.
I want to ask about best practice how you guys do when saving and updating data to database using array of object.
例如,我有这个AoJ:
For example i have this AoJ:
[
{
package_id: 1,
posts: [
{id: 1, post_id: "1d1479c6-c114-46d5-becd-e4715c14e57d", name: "Post 1", price: 3000}
{id: 2, post_id: "84f37e37-d050-4efd-bd08-811ab893959f", name: "Post 2", price: 3000}
{id: 3, post_id: "1d1479c6-c114-46d5-becd-e4715c14e57d", name: "post 3", price: 3000}
{id: 4, post_id: "84f37e37-d050-4efd-bd08-811ab893959f", name: "post 4", price: 3000}
]
}
];
对于每个数组,我将在数据库上使用新ID创建新行.
For every array I will create a new row on database with new ID.
这就是我想要的
如果我要删除ID 3,如何更新数据? 检测某些记录应从包装中删除的最佳实践是什么?
How to update the data if i want to remove id 3? What is the best practice to detect that some record should be removed from package?
目前,我的解决方案是:
For now my solution is:
[
{
package_id: 1,
posts: [
{id: 1, post_id: "1d1479c6-c114-46d5-becd-e4715c14e57d", name: "Post 1", price: 3000, remove: false}
{id: 2, post_id: "84f37e37-d050-4efd-bd08-811ab893959f", name: "Post 2", price: 3000, remove: false}
{id: 3, post_id: "1d1479c6-c114-46d5-becd-e4715c14e57d", name: "post 3", price: 3000, remove: true}
{id: 4, post_id: "84f37e37-d050-4efd-bd08-811ab893959f", name: "post 4", price: 3000, remove: false}
]
}
];
因此,在更新package_id 1帖子时,如果看到remove: true
它将删除它.
So on updating package_id 1 posts when it see a remove: true
it will remove it.
你们怎么看?有什么建议可以使其更简单吗?
What do you guys think? Any suggestion to make it more simple?
NB:我不能使用删除所有内容并发布新的一种方法.因为每个package_id帖子都与其他表有关系
NB: I can't use delete everything and post new one method. because every package_id posts has relationship to other table
推荐答案
正如我们所讨论的,让我发布一个基本的解决方案.
As we discussed, let me post a basic solution to it.
我们有一个类似Post的模型
We have one Post model like
class Post extends Model{}
另一个是包模型
class Package extends Model{}
如果两个都有主键"id",
if both have primary key 'id',
描述它们之间的多对多关系的数据透视表将是
the pivot table describing many-to-many relationship between them will be
package_id,post_id组合为一个复合主键,类似于
package_id, post_id combinly as a composite primary key, something like
Schema::create('package_post', function (Blueprint $table) {
$table->unsignedInteger('package_id');
$table->unsignedInteger('post_id');
$table->foreign('package_id')->references('id')->on('packages')
->onUpdate('cascade')->onDelete('cascade');
$table->foreign('post_id')->references('id')->on('posts')
->onUpdate('cascade')->onDelete('cascade');
$table->primary(['package_id', 'post_id']);
});
根据要求,可能还会有其他列.
There may be some other columns, depending on requirement.
这种关系会像
class Package extends Model {
public function posts()
{
return $this->belongsToMany( Post::class, 'package_post', 'package_id', 'post_id');
}
}
和
class Post extends Model {
public function packages()
{
return $this->belongsToMany( Package::class, 'package_post', 'post_id', 'package_id');
}
}
现在,当您要检索或编辑或删除它时,可以使用附加,分离等方法.
Now when you want to retrieve it or edit it or delete it, you can use attach, detach etc methods.
对于检索,如果程序包id = 1,并且您要检索该程序包的所有帖子,则可以像这样检索它
For retrieve, if package id = 1 and you want to retrieve all posts belongs to this package, you can just retrieve it like
$posts= Package::find(1)->posts;
对于插入,您可以使用
$package->posts()->attach($post->id);
要更新
$package->posts()->sync([$post_id]);
如果要将post_id 1更改为2
if you want to change post_id 1 to 2
$package->posts()->wherePivot('post_id', 1)->sync(2);
要分离
$package->posts()->detach($post_id);
检查关系是否存在
$package->posts->contains($post_id)
这篇关于Laravel-最佳实践更新,并使用对象数组将记录保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!