Bamboo is a package for Laravel version 4 that enables scaffolding for Eloquent models.
-
Add
"RobGordijn/Bamboo": "dev-master"
to the require section incomposer.json
. -
Run
php composer update
on the CLI. -
Add
'RobGordijn\Bamboo\BambooServiceProvider'
to the providers array inapp/config/app.php
. -
In the
controllers
directory, create a new controller that extends the BambooController and provides an Eloquent model in the constructor.
<?php
use RobGordijn\Bamboo\BambooController;
class BlogController extends BambooController
{
public function __construct(Blog $Model)
{
parent::__construct($Model);
}
}
- Create a route to that controller.
<?php
// a single route
Route::resource('blogs', 'BlogController');
// or within in a group with a prefix
Route::group(array('prefix' => 'admin'), function()
{
Route::resource('blogs', 'BlogController');
});
- The last step is to provide information about the structure of your model. This is done via a public method
getStructure
in the Eloquent model.
<?php
class Blog extends Eloquent
{
protected $table = 'blogs';
protected $fillable = array('title', 'content');
public static $rules = array(
'title' => array('required')
,'content' => array('required')
);
public function getStructure()
{
$title = array(
'type' => 'text'
,'onIndex' => true
);
$content = array(
'type' => 'text'
);
return compact('title', 'content');
}
}
- done; point your browser to
/blogs
to work with Bamboo.
Please keep in mind that the Laravel Eloquent model attributes are well protected for unwanted mass-assignment. Check the documentation on how to set the fillable attributes.
Hint: protected $fillable = array('column1', 'column2');
or inversed like protected $guarded = array();
I advice you to use the first option.
The getStructure
method in the Eloquent model must return an array of structures. The keys are the names of your table columns. The structure has the following options:
key | value | default | description |
---|---|---|---|
type | (string) text, email, password, hidden | text | single line text input |
(string) textarea | textarea input | ||
(string) radios | list of radio buttons, requires 'values' | ||
(string) select | dropdown menu, requires 'values' | ||
label | (string) | null | String used for the label |
rules | (array) | empty array | Array with model rules, not implemented yet, use static Model::$rules meanwhile |
onIndex | (bool) | false | Display column on the index view, default: false |
editable | (bool) | true | Column can be editted, default: true |
attributes | (array) | empty array | Array with attributes used in the formbuilder |
values | (array) | empty array | Array with possible values for type 'radios' and 'select' |
Records per page (index view)
Default: 10, specify the protected recordsPerPage
property in the resource controller to overwrite:
protected $recordsPerPage = 25;
Ordering records on the index view
Default: Model->getKeyName() asc
, specify the protected orderByColumn
and orderByDirection
in the resource controller to overwrite.
protected $orderByColumn = 'title';
protected $orderByDirection = 'desc';
Blade layout
Every view uses a Blade layout to render. A default layout is shipped with Bamboo and uses Bootstrap classes to look nice. Specify the protected bladeLayout
property in the resource controller to overwrite.
protected $bladeLayout = 'layouts/master';
Bamboo ships in English, feel free to contribute.
What about validation?
Specify the rules for the columns in the static property rules
, Bamboo will pick them up when storing or updating records.
Do I need to worry about the url of the resource controller?
Nope, as long as you register the route to the controller with Route::resource()
, Bamboo will figure out the rest.
Does Bamboo generate controllers and views like Jeffrey Way's Laravel 4 Generators?
Nope, Bamboo does not generate the controllers and views for each model but reuses one controller and some views.
- 2014-04-09
bootstrap update, general update because nothing worked - 2013-09-26
removed type 'string' and swiched it with 'text' so the only way to render a textarea is the type 'textarea'. - 2013-09-25
added active class for the nav-tabs on the index view
added overwrites for orderByColumn (default: table key) and orderByDirection (default: asc) - 2013-09-23
initial version commit