Wanna Use a Database?
A few months ago we added the ability to create your own custom Publish Forms. It joined the rank and file of other useful-but-addon-developer-specific features that faded into the background. This post aims to shine the light on this incredibly powerful feature 🔦.
If you are Laravel developer or are migrating from another platform and have some DB tables full of data you don’t want to migrate to flat files, or some other similar use case, this article is for you.
Out-of-the-box Statamic
Typically, content is stored in YAML Front-Loaded Markdown files. When you edit content in the Control Panel, the data is loaded into a standardized object structure, injected into our Vue-powered Publish
component, is available to be manipulated by your custom fields, and then is transformed back into that YAML Front-Loaded Markdown format again on save.
However, if you want your data to live somewhere else, you can write your own load/save logic and let the Publish component do all the hard work. We have a page dedicated to the Publish component in our docs, but sometimes it’s better to see stuff in action. So, we built two real-world use cases/demos for you to play around with. Feel free to use and modify them however you’d like!
Laravel Eloquent Version
Eloquent, included with Laravel, provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding “Model” which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table. You can learn more about Eloquent in the Laravel docs. Just keep in mind: Statamic 2.x runs on Laravel 5.1.
We built an addon that hooks up a generic Product
Eloquent model/table to a custom Publish component in its own area of the Control Panel. The package also includes templates, routes, and tags that allow the products to be displayed on the front-end in standard Statamic style.
Check out the repo here (docs in the README):
Custom Publish Forms: Eloquent Edition
Third Party API Version
Another use case may be to publish content to a completely different app via an API. You might want to do this if you have an existing Laravel app and want to manage content in your Statamic site. Just an idea.
This addon works the same way as the Eloquent example, except the data is stored in completely separate site. This repo holds two separate apps:
- The Statamic site that pulls in content from another app.
- A very basic Laravel app that acts as the “third party”.
Here’s the repo (docs in the README):
Custom Publish Forms: API Edition
Tips
Here are a few tips if you decide to experiment with this approach.
Each model needs a corresponding fieldset with fieldnames that match the column names.
Be sure to whitelist the columns you want Statamic to be able to manipulate.
Database credentials are stored in your
.env
file.
DB_HOST=localhost
DB_DATABASE=database-name
DB_USERNAME=username
DB_PASSWORD=password
- Go nuts and show us what you build!