vendredi 25 décembre 2015

How to create a model this relationship in Laravel 5?

I have the following schema:

Modules
=======
id: PK
current_revision: FK to Revisions.Id, nullable

Revisions
=========
id: PK

The current_revision field either references to a single revision id or set to null.

My question is how do I properly model this relationship in Laravel.

Here's how I did it so far:

class Module extends Model
{
    public function current_revision()
    {
        return $this->belongsTo(Revision::class, 'current_revision');
    }

}

However, I did not do the inverse in the revision model, as there can be revisions with no module assigned to it.

Is this the correct way to model this relationship?

Also why does the following $module->current_revision not output a collection of revisions? Instead it outputs the contents of the field.

However it's worth mentioning the following works fine: $module->current_revision()->getResults()



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire