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.
We recommend reading the docs on Eloquent & Schema. Below is quick overview.
use Illuminate\Database\Capsule\Manager as Capsule;
Capsule::schema()->create('orders', function($table)
{
$table->increments('id');
$table->string('title');
});
use Illuminate\Database\Capsule\Manager as Capsule;
Capsule::schema()->dropIfExists('orders');
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;
}
Order::create(['title' => 'Wii U']);
$order = Order::first();
$order->title = 'Playstation 4';
$order->save();
To make your life easier we've include some Models to help you work with the standard WordPress tables. These include:
Herbert\Framework\Models\Comment;
Herbert\Framework\Models\CommentMeta;
Herbert\Framework\Models\Option;
Herbert\Framework\Models\Post;
Herbert\Framework\Models\PostMeta;
Herbert\Framework\Models\Taxonomy;
Herbert\Framework\Models\Term;
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]);
}
}