Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

relationship model uuid #33

Closed
robertnicjoo opened this issue Aug 21, 2020 · 7 comments
Closed

relationship model uuid #33

robertnicjoo opened this issue Aug 21, 2020 · 7 comments
Labels
question Further information is requested

Comments

@robertnicjoo
Copy link

I have relationship table that connects 2 other tables together (let say post _tags) this table only gets id of post and id of tags therefore this table does not have model (doesn't need one), now when I store my post tags with sync() method. It leaves id column empty which cause errors while storing my posts.

Any solution to that?

@matthew-inamdar matthew-inamdar added the question Further information is requested label Aug 26, 2020
@matthew-inamdar
Copy link
Member

@robertnicjoo am I right in understanding you have the following?

  • Post model
  • Tag model
  • post_tags pivot table

If so, what model is not having the id column value generated?

@robertnicjoo
Copy link
Author

@robertnicjoo am I right in understanding you have the following?

* `Post` model

* `Tag` model

* `post_tags` pivot table

If so, what model is not having the id column value generated?

this table post_tags (ps: doesn't have a model)

@matthew-inamdar
Copy link
Member

@robertnicjoo so does the post_tags table have an id column? Or is it a composite primary key of the post_id and tag_id columns?

@robertnicjoo
Copy link
Author

Yes it does have column id , and no it's not composite primary key

@matthew-inamdar
Copy link
Member

@robertnicjoo I wouldn't expect this to work as you think, as it's ran as part of the boot() hook for a model. And since your pivot table isn't a model, it wouldn't have a boot() method to run.

As part of the sync() method, you can associate the ID with an array of attributes for the pivot table record, in which you could include the generated UUID using Ramsey\Uuid\Uuid\uuid4()->toString():

$post->tags()->sync([
    $tag1->id => ['id' => Ramsey\Uuid\Uuid\uuid4()->toString()],
]);

Screenshot 2020-08-26 at 22 03 47

Source: https://laravel.com/docs/7.x/eloquent-relationships

@robertnicjoo
Copy link
Author

Thank you I will try this.

@robertnicjoo
Copy link
Author

Working codes

example 1 similar to @matthew-inamdar code

$tags_id = (array) $request->input('tags');
foreach( $tags_id as $tag_id ) {
  $post->tags()->sync([
    $tag_id => ['id' => \Ramsey\Uuid\Uuid::uuid4()->toString()],
  ]);
}

example 2

$tags_id = (array) $request->input('tags');
foreach( $tags_id as $tag_id ) {
  $tag_data_to_sync[ $tag_id ] = [ 'id' => \Ramsey\Uuid\Uuid::uuid4()->toString() ];
}
$post->tags()->sync( $tag_data_to_sync );

Note
If in any case your tag ids come to controller as strings like this

"86198680-1f0b-4544-851a-4c39e4f7c3ec,2bbe9776-7413-43fa-9cae-3378f39f3c2d"

You need to explode them first before you loop them

$tags_id = explode(',', $request->input('tags'));
foreach( $tags_id as $tag_id ) {
  ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Development

No branches or pull requests

2 participants