Giter Site home page Giter Site logo

Comments (7)

matthew-inamdar avatar matthew-inamdar commented on May 12, 2024 4

@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

from laravel-eloquent-uuid.

matthew-inamdar avatar matthew-inamdar commented on May 12, 2024

@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?

from laravel-eloquent-uuid.

robertnicjoo avatar robertnicjoo commented on May 12, 2024

@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)

from laravel-eloquent-uuid.

matthew-inamdar avatar matthew-inamdar commented on May 12, 2024

@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?

from laravel-eloquent-uuid.

robertnicjoo avatar robertnicjoo commented on May 12, 2024

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

from laravel-eloquent-uuid.

robertnicjoo avatar robertnicjoo commented on May 12, 2024

Thank you I will try this.

from laravel-eloquent-uuid.

robertnicjoo avatar robertnicjoo commented on May 12, 2024

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 ) {
  ...
}

from laravel-eloquent-uuid.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.