R
R
Roman Sarvarov2020-07-29 17:17:49
Laravel
Roman Sarvarov, 2020-07-29 17:17:49

How to make correctly polymorphic connections and relationships?

There are 3 models: Document, Status, ApprovalAttempt.

documents
    id - integer
    name - string

statuses
    id - integer
    approval_attempt_id - integer (foreign key)

approval_attempts
    id - integer
    approvable_type - string
    approvable_id - integer


In approval_attempts I use the fields to create a Laravel polymorphic relationship.

How to make it possible to call a property in the Document model ->statusesand get all document statuses (document ID in approval_attempts, ID ApprovalAttempt in the statuses table).

Like this:
public function statuses()
{
    return $this->approvalAttempts()->statuses();
}

But it's probably too sugary and doesn't work. How to be?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir, 2020-07-30
@mvs

Look towards hasMany and belongsTo. Well, the documentation in general https://laravel.com/docs/7.x/eloquent-relationships

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question