douhan0562 2019-03-11 07:13
浏览 368
已采纳

Laravel - 使用对象数组更新并将记录保存到数据库

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:

[
    {
        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.

Here is what I want

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}
        ]
    }
];

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: I can't use delete everything and post new one method. because every package_id posts has relationship to other table

  • 写回答

1条回答 默认 最新

  • dongxuan58311366668 2019-03-11 08:46
    关注

    As we discussed, let me post a basic solution to it.

    We have one Post model like

    class Post extends Model{}
    

    another is Package model

    class Package extends Model{}
    

    if both have primary key '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

    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.

    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;
    

    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

    $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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大