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
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.