How would i make a single request to insert in multiple tables using laravel Eloquent ORM relationship .ie
Table 1 : users
- id
- name
Table 2 : posts
- id
- user_id
- content
Table 3 : Image
- id
- user_id
- post_id
- image_name
relationship
- Table
users
id referencesuser_id
in other two tables . - Table
posts
has One To Many relation withusers
. - Table
images
has One To Many relation withusers
andpost
ie it can be shared with other users and on other posts.
So that when one makes a post insert ,it should insert the record in the tables with a single query.
This is one way of doing it:
As for the image, the
Post
model should have a model event such asstatic::created
to handle the image upload and manipulation.Or to make more sense, a model event in the
Post
model should trigger another model event from theImage
model.->toArray()
may be optional, I can't test it here where I'm now.