简体   繁体   English

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. 我想问一问最佳实践,你们在使用对象数组将数据保存和更新到数据库时如何做。

For example i have this AoJ: 例如我有这个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}
        ]
    }
];

For every array I will create a new row on database with new ID. 对于每个数组,我将在数据库上使用新ID创建新行。

Here is what I want 这就是我想要的

How to update the data if i want to remove id 3? 如果我要删除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}
        ]
    }
];

So on updating package_id 1 posts when it see a remove: true it will remove it. 因此,在更新package_id 1帖子时看到remove: true它将删除它。

What do you guys think? 你们有什么感想? Any suggestion to make it more simple? 有什么建议可以使其更简单吗?

NB: I can't use delete everything and post new one method. 注意:我不能使用删除所有内容并发布新的一种方法。 because every package_id posts has relationship to other table 因为每个package_id帖子都与其他表有关系

As we discussed, let me post a basic solution to it. 正如我们所讨论的,让我发布一个基本的解决方案。

We have one Post model like 我们有一个Post模型,例如

class Post extends Model{}

another is Package model 另一个是包装模型

class Package extends Model{}

if both have primary key 'id', 如果两个都有主键“ id”,

the pivot table describing many-to-many relationship between them will be 描述它们之间多对多关系的数据透视表将是

package_id, post_id combinly as a composite primary key, something like package_id,post_id组合为复合主键,类似于

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. 根据需要,可能还有其他一些列。

The relationship will be like 关系会像

class Package extends Model { 

 public function posts()
    {
        return $this->belongsToMany( Post::class, 'package_post', 'package_id', 'post_id');
    }
}

and

 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. 现在,当您要检索或编辑或删除它时,可以使用attach,detach等方法。

For retrieve, if package id = 1 and you want to retrieve all posts belongs to this package, you can just retrieve it like 为了进行检索,如果程序包id = 1,并且您要检索该程序包的所有帖子,则可以像这样检索它

$posts= Package::find(1)->posts;

For insert, you can use 对于插入,您可以使用

$package->posts()->attach($post->id);

To update 更新

$package->posts()->sync([$post_id]);

if you want to change post_id 1 to 2 如果要将post_id 1更改为2

$package->posts()->wherePivot('post_id', 1)->sync(2);

To detach 分离

$package->posts()->detach($post_id);

To check if the relationship exists 检查关系是否存在

$package->posts->contains($post_id)

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

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