Be warned: This is a work in progress. Nothing actually works yet!
Add nils-werner/scaffold
as a requirement to composer.json:
{
"require": {
"nils-werner/scaffold": "dev-master"
},
"repositories": [
{
"type":"vcs",
"url":"https://github.com/nils-werner/laravel-scaffold"
}
]
}
Then run composer update
or composer install
Next step is to tell laravel to load the serviceprovider. In app/config/app.php
add
// ...
'NilsWerner\Scaffold\ScaffoldServiceProvider'
// ...
to the providers
array.
After installing, pick any of your existing Eloquent
models and view its scaffolding view by navigating to
http://host/scaffold/<handle>
where <handle>
is the lowercase name of your model. See section Configuration
if any of your table fields
contain special data like a file upload or relations.
This bundle primarily exists of the following parts:
routes.php
controllers/ScaffoldController.php
views/*.blade.php
NilsWerner/Scaffold/Fields/*
It's in ScaffoldController
where most of the magic happens:
- Deduct the model name from the URL and see if it exists
- Fetch the schema of the table associated with the model
- Process and display the data and the schema in a table or a form
All files in NilsWerner/Scaffold/Fields/
contain the specific codebase for each fieldtype to be displayed
and processed.
Each Model
can configure an array of columns and fields it wants to show in the scaffolding view:
public $columns = ['realname'];
public $fields = ['realname', 'email'];
You can also manipulate the type of input field and logic to be used for each field:
public $fields = ['realname', 'email', 'avatar' => 'upload', 'active' => 'checkbox'];
This will ask NilsWerner\Scaffold\Fields\Upload
and NilsWerner\Scaffold\Fields\Checkbox
to render the input view, as opposed to the default NilsWerner\Scaffold\Fields\String
etc.
Relations can be showed by simply using the relation
type in your Model fields:
public $fields = ['user' => 'relation', 'comments' => 'relation', 'title', 'body'];
The type of relation will automatically be figured out. The name of the field must represent the relation method used in the Model.
In order to implement new field types, you need to create a Class that inherits from NilsWerner\Scaffold\Fields\Field
:
class Somefield extends NilsWerner\Scaffold\Fields\Field {
public function input()
{
return Illuminate\Support\Facades\Form::checkbox($this->handle());
}
}
and bind it to the alias you want to use:
App::bind('Scaffold\Fields\Somefield', function($app, $handle)
{
return new Fields\Somefield($app, $handle);
});
You can then use the type somefield
in the $fields
array in your model.
Additionally, you can use Eloquent ORM getters and setters to manipulate the input received from the form, i.e. Hash::make()
a password when saving and only display a blank field when editing:
public function setPasswordAttribute($value)
{
if($value != "")
$this->attributes['password'] = Hash::make($value);
}
public function getPasswordAttribute($value)
{
return "";
}