eloquent-driver
eloquent-driver copied to clipboard
Use different Eloquent models for different collections
I would really like to use Statamic in a project with a traditional datbase layout, but it looks like it's impossible or very difficult.
I have tried following the code in this repository (and the guide in the knowledge base) to implement this myself. It seemed promising since Statamic uses a repository, but currently this driver (and Statamic in general) assumes that all entries, in any collection, are stored in a single database table. This approach works when using a JSON data column, but fails when trying to apply it to a more traditional database layout where different kinds of content are stored in different tables.
Also when trying to see if I could somehow get around this limitation by making a clever QueryBuilder implementation I noticed that many of the interfaces (like Statamic\Contracts\Entries\QueryBuilder) are empty, so it's difficult to know what methods need to be implemented.
Anyway, is this feature even possible to implement?
Great feature request. Maybe a duplicate of https://github.com/statamic/eloquent-driver/issues/11 though?
They are similar, but the feature discussed in #11 would still use the same Eloquent model/database table for the selected collections, whereas what I am proposing here is that it would also be possible to specify a Model per collection.
Fair enough, will leave open.
Is this not exactly what the Runway add-on lets you do? https://statamic.com/addons/double-three-digital/runway
Hey folks. I'm also REALLY needing this.
In my case, all I need is to have each collection in its own table. There's no need for each field to be in a separate column.
So, my idea was this:
When a new collection is created, we only need to create the table to hold the entries for that collection using the same structure, what can even be done at runtime using Laravel's Schema Builder. This way, we can have each collection in a separate table, but still have all the benefits and flexibility of Statamic's blueprints without having to create migrations every time ou change our blueprint structure, since every table will have the same structure and data coming from the form will still be stored at a data column, just like it's already done in the entries table.
Then, I thought to simply use the same EntryModel for every table, but set the table for the model at runtime using the collection handle and eloquent's setTable method. This will prevent us from having to create a specific model for each table every time we create a new collection.
At first I thought I could simply tweek the EntryRepository and the EntryQueryBuilder. However, it turned into a 2 day code digging nightmare where I couldn't find where the EntryQueryBuilder sets the table it uses and where to set the table for the models was even more challenging and unsuccessful.
High heavens frustration levels.
Does anyone have some sort of direction to this?
For this use case I highly recommend using Runway