mardi 21 mai 2019

Laravel: difference between using a Trait or extending a model

We're working on a Laravel application with a scheduling module. The module has three types of classes that can be put in an agenda: Task, Event and Department. We've therefore come up with the following class diagram: Class Diagram Now, our question: if we were to realise this diagram, should we use a Trait or should we extend a Plannable model.

Plannable model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Plannable extends Model
{
    // Code
}

Task model:

<?php

namespace App;

use App\Plannable;

class Task extends Plannable
{
    // Code
}

Or should we use this as a trait:

Plannable trait:

<?php

namespace App\Traits;

trait Plannable
{
    // Code
}

Task model:

<?php

namespace App;

use App\Plannable;
use Illuminate\Database\Eloquent\Model;

class Task extends Model
{
    use Plannable;
    // Code
}

Aucun commentaire:

Enregistrer un commentaire