Saving Data

Herbert pulls in Laravel's Eloquent ORM. However, not all plugins will need this. If you plan to save very little information you will want to look into WordPress Options Mechanism.

Eloquent ORM

We recommend reading the docs on Eloquent & Schema. Below is quick overview.

Creating a Table

use Illuminate\Database\Capsule\Manager as Capsule;

Capsule::schema()->create('orders', function($table)

Dropping a Table

use Illuminate\Database\Capsule\Manager as Capsule;


Create Model for Table

Models are typically stored in the app/Models directory. Below we create Orders.php

<?php namespace MyPlugin\Models;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Order extends Eloquent {
    protected $fillable = ['title'];
    public $timestamps = false;

Create Record

Order::create(['title' => 'Wii U']);

Get first record and update it

$order = Order::first();
$order->title = 'Playstation 4';

WordPress Models

To make your life easier we've include some Models to help you work with the standard WordPress tables. These include:


An example of using one of these to get a Post by id.

<?php namespace MyPlugin\Controllers;

use Herbert\Framework\Models\Post;

class PostController {

     * Show the post for the given id.
    public function showPost($id)
        $post = Post::find($id);

        return view('@MyPlugin/post/single.twig', ['post' => $post]);