Skip to content

nfragkos/statamic-eloquenty

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

94 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Eloquenty Statamic Addon

MIT Licensed

This package allows you to store entries for specific collections to the database via Laravel's Eloquent ORM.

Structures are disabled for performance reasons.

Requirements

  • PHP 8+
  • Statamic v4

How it works

Eloquenty uses Statamic Eloquent Driver (https://github.com/statamic/eloquent-driver) but with modifications that allows to use the driver for specific collections entries instead for every entry.

Screenshot 2021-01-27 162333 Both standard and Eloquenty collections will be visible under /cp/collections but the Entries column will display the message "Managed by Eloquenty" for Eloquenty collections.

Screenshot 2021-01-27 162410 Eloquenty collections are managed on a separate route /cp/eloquenty/collections which is a clone of /cp/collections route group with modifications to use the Entry and EntryRepository classes under Eloquenty\Entries that uses Eloquent ORM. The Entry related classes and Controllers are extending the original Statamic classes.

There's a middleware that redirects from /cp/collections/{collection} to /cp/eloquenty/collections/{collection} when clicking to view Eloquenty collections under /cp/collections.

Collection modifications should be performed under the original /cp/collections route.

Installation

You can install this package via composer using:

composer require nfragkos/eloquenty

The package will automatically register itself.

Post-Install

  1. Publish eloquenty config and migration files:

    php artisan vendor:publish --provider="Eloquenty\ServiceProvider"

  2. Run php artisan migrate to create Eloquenty table.
    (Make sure laravel is configured properly and schema is created. See https://laravel.com/docs/8.x/database#configuration)

  3. Create a new collection or enter existing collection handle in config/eloquenty.php:

    'collections' => [
        'blog',
    ],

    This means that the entries for blog collection will be handled by Eloquenty.

  4. If you entered an existing collection to Eloquenty config, there's an import command to import entries to the database:

    php artisan eloquenty:import-entries blog

    Please delete your collection entries from the filesystem after you verify that entries are imported successfully.

Usage

Eloquenty now should be visible now under Content section of the navigation menu in Control Panel. CRUD for Eloquenty collections works the same way as standard collections.

Antlers

In Antlers templates use the eloquenty tag instead of the collection tag for Eloquenty collections:

{{ eloquenty from="blog" limit="10" taxonomy:tags="demo|example" sort="date:desc" }}
    <a href="{{ url }}">{{ title }}</a>
{{ /eloquenty }}

Data retrieval and manipulation

Calling the queryEntries() method of Collection repository, will return the correct QueryBuilder for Eloquenty collections:

\Statamic\Facades\Collection::find('blog')->queryEntries()

Alternatively you can use the Facade Eloquenty::repository() method or EloquentyEntry::query() to retrieve the instance of EntryRepository (implements Statamic\Contracts\Entries\EntryRepository):

Eloquenty\Facades\Eloquenty::repository()
Eloquenty\Facades\EloquentyEntry::query()

Keep in mind that calling the whereCollection() method will fetch all entries in the table. You should use the query() method instead that returns the query builder instance to build your query.

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Pull requests are welcome and will be fully credited.

  1. Fork the repo and create your branch from main branch.
  2. Update the README file if needed.
  3. Make sure your code follows the PSR-2 coding standard.
  4. Issue that pull request!

License

The MIT License (MIT). Please see License File for more information.

Credits

Many thanks to Statamic for the Eloquent driver and the awesome CMS ❤️

Coffee

If you like this addon please consider buying me a coffee:

paypal