S
S
Stanislav Pochepko2016-05-08 23:17:16
MySQL
Stanislav Pochepko, 2016-05-08 23:17:16

How to implement history many-to-many relationship in laravel?

Good evening. Prompt how correctly to implement communication between models. I have an Apply request model and a Status model. A many-to-many relationship (belongsToMany). The link table has additional fields such as comment, user_id. It is necessary to make the connection in such a way that I could access the current status through the magic property $Apply->statusand use

$Apply->status()->where('...', '...')->get() // и тому подобное
.
But there is also the possibility of $Apply->statusesdoing
$Apply->statuses()->where('...', '...')->get() // и тому подобное

As far as I understand, Laravel does not provide such functionality, or I do not know it. How correct is it to use the 4th table apply_status_history in which the status records will be recorded (duplicated), and overwritten in the binder?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Pochepko, 2016-05-10
@DJZT

Solved the issue by double linking. Made a table for the StatusHistory model

status_history
- increment id
- integer status_id
- integer apply_id
- integer user_id
- string comment
- timestamps

applies
- increment id
- .....
- status_id

apply_statuses
- increment id
- string title

I passed the parameters through getCommentAttribute and getTitleAttribute from the Status model to the StatusHistory model and added a handler for saving the Apply model so that the status would be duplicated in the StatusHistory table

A
Andrzej Wielski, 2016-05-09
@wielski

All data from the pivot table is stored in pivot

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question