luddinus's avatar

Eloquent Model does too much.

I think there should be two different Eloquent Classes. One for the "table" (e.g BaseRepository) and one for the Record. I mean, I think it shouldn't be possible to do this:

$user = User::find(1); // I have a user "record".

// get all the users with the current "record". In my opinion, 
// the "User record" shouldn't be allowed to use the "all" method.
$users = $user->all(); // returns Collection

I mean, I'm getting a collection "from" one "record".

What do you think? Is Eloquent against the principle of One Class One "Task"? (I dunno how to explain)

0 likes
2 replies
pmall's avatar

This is the basic active record pattern. This is a trade-of between simplicity and functionality.

As you said you can simulate what you want by using a repository.

Keep in mind you can also use custom collections to extends the functionality of a model's collection.

use Illuminate\Database\Eloquent\Collection;

class CustomCollection extends Collection{
  // Custom methods
}
use App\Collections\CustomCollection;
use Illuminate\Database\Eloquent\Model;

class YourModel extends Model{

  public function newCollection(Array $models = [])
  {
    return new CustomCollection($models);
  }

}

Finally there are packages which allow you to use the data mapper pattern. Analogue seems pretty cool I plan on testing it someday.

Please or to participate in this conversation.