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

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

报告相同问题?

悬赏问题

  • ¥30 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目
  • ¥15 数电几道习题,写出作答过程,ai一律不采用
  • ¥15 利用pthon计算薄膜结构的光导纳
  • ¥15 海康hlss视频流怎么播放
  • ¥15 Paddleocr:out of memory error on GPU
  • ¥30 51单片机C语言数码管驱动单片机为AT89C52