Comments (7)
@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()],
]);
Source: https://laravel.com/docs/7.x/eloquent-relationships
from laravel-eloquent-uuid.
@robertnicjoo am I right in understanding you have the following?
Post
modelTag
modelpost_tags
pivot table
If so, what model is not having the id
column value generated?
from laravel-eloquent-uuid.
@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.
@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.
Yes it does have column id
, and no it's not composite primary key
from laravel-eloquent-uuid.
Thank you I will try this.
from laravel-eloquent-uuid.
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)
- Missing documentation for migrations table HOT 10
- uuid:make:model fails in case sensitive filesystems HOT 2
- Alternative trait instead of extending class HOT 2
- Laravel 7 Support HOT 1
- Backport Uuid trait to Laravel v5.8 HOT 3
- General error: 20 datatype mismatch HOT 1
- Integrity constraint violation: 19 NOT NULL HOT 4
- Create UUID in seeder HOT 5
- Is there any need to have this? HOT 1
- Event::fake() in tests prevents uuid being automatically generated HOT 2
- Model Injection not working
- Laravel 8 Compatibility HOT 1
- Duplicate HOT 2
- 419 Page Expired HOT 2
- doesn't work with method 'with' HOT 3
- UUID on pivot HOT 3
- php8 compatibility HOT 2
- $guarded must be array HOT 2
- Laravel 9 HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-eloquent-uuid.