Wallogit.com
2017 © Pedro Peláez
Laravel Controller extended, to easier create crud page., (*1)
Add to composer.json, (*2)
{
"require": {
"timenz/crud": "dev-master"
}
}
To add support WYSIWYG and image field type add this other two repository that forked from guillermomartinez and kevbaldwyn, and the composer requirement become:, (*3)
{
"require": {
"timenz/crud": "dev-master",
"timenz/filemanager-laravel": "dev-l5.1",
"timenz/image":"dev-l5.1"
}
}
then update, (*4)
composer update
add to $app['providers'], (*5)
\Timenz\Crud\CrudServiceProvider::class, \Pqb\FilemanagerLaravel\FilemanagerLaravelServiceProvider::class, \KevBaldwyn\Image\Providers\Laravel\ImageServiceProvider::class,
and to $app['aliases'], (*6)
'ImageSrc' => \KevBaldwyn\Image\Providers\Laravel\Facades\Image::class,
publish view and asset, (*7)
php artisan vendor:publish
Javascript Libs Dependency, (*8)
{
"bootstrap": "~3.3.0",
"bootstrap-datepicker": "1.3.0",
"chosen": "~1.4.2",
"eonasdan-bootstrap-datetimepicker": "~4.17.37"
}
Master blade view, this view basically from bootstrap sample page., (*9)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>Crud Master Example</title>
<link href="{{ asset('libs/bootstrap/dist/css/bootstrap.min.css') }}" rel="stylesheet">
<link href="{{ asset('libs/bootstrap-datepicker/css/datepicker3.css') }}" rel="stylesheet">
<link href="{{ asset('assets/css/bootstrap-chosen.css') }}" rel="stylesheet">
<link href="{{ asset('assets/css/navbar-fixed-top.css') }}" rel="stylesheet">
@yield('crud_css')
</head>
<body>
<div class="container-fluid">
@yield('crud_konten')
</div>
<script src="{{ asset('libs/jquery/dist/jquery.min.js') }}"></script>
<script src="{{ asset('libs/bootstrap/dist/js/bootstrap.min.js') }}"></script>
<script src="{{ asset('libs/chosen/chosen.jquery.min.js') }}"></script>
<script src="{{ asset('libs/bootstrap-datepicker/js/bootstrap-datepicker.js') }}"></script>
@yield('crud_js')
</body>
</html>
create resource route, (*10)
Route::resource('article', 'ArticleCrud');
then create controller, (*11)
<?php
namespace App\Http\Controllers;
use Timenz\Crud\Crud;
class ArticleCrud extends Crud{
protected function run(){
$this->init('article', 'crud_master', []);
return true;
}
}
[wajib] Main init, (*12)
// parameter => table name, master blade and parameter for master blade
$this->init('users');
set crud title //parameter => crud title $this->setTitle('Article Crud');, (*13)
Limit permission of crud, (*14)
//parameter L = list
$this->disAllow('LCREDSXO');
Specify fields in listing at index page., (*15)
//parameter => array of fields
$this->columns(array(
'title',
));
Specify validate rules, rules that used in this validation is same as default laravel validation rules and you can follow reference from laravel documentation, (*16)
$this->validateRules(array(
'title' => 'required',
));
Specify fields at create, edit dan read page, (*17)
$this->fields(array(
'title',
));
Modify title of field, (*18)
$this->displayAs('title', 'The Title');
Specify fields at create page, (*19)
$this->createFields(array(
'title',
));
Specify fields at edit page, (*20)
$this->editFields(array(
'title',
));
Specify fields at read page, (*21)
$this->readFields(array(
'title',
));
Filters visible data by using sql where, (*22)
//parameter => field, condition, value
$this->where('id', '=', 1);
Set a 1-n database relationship, this function only can be use for relation that refers to another table on field 'id', example field 'id_user' from initial table refers to field 'id' on target table., (*23)
// parameter
// 1. field from initial table that refers to target table
// 2. another table that want to join to
// 3. field name from refered table that want to set visible on initial field
// 4. 'where' array to limit join data
$this->setJoin('id_user', 'users', 'full_name');
Set order listing data, (*24)
//parameter 1. field name, 2. sort (asc/desc)
$this->orderBy('id', 'asc');
Set a custom column value in listing data, (*25)
$this->callbackColumn('field_name', function($row, $val){
return number_format($val);
});
Provide some operation before saving data, (*26)
$this->callbackBeforeSave(function($post_data){
$post_data['password'] = Hash::make($post_data['password']);
return $post_data;
});
Provide some operation before update data, (*27)
$this->callbackBeforeUpdate(function($post_data){
$post_data['password'] = Hash::make($post_data['password']);
return $post_data;
});
Add custom action other than read, edit and delete, (*28)
addAction('Action', 'btn btn-success', function($row_data, $id){
return url('action/'.$id);
})
Add static link to crud page, (*29)
addExternalLink('Custom Link', url('link'));
By default package will set the field type by type at table definition, but we can change the field type to another custom field type. This function has 3 parameter., (*30)
To avoid coding error, I have created two classes to specify available change type and change type option parameter., (*31)
This is third parameter of changeType function, here some available ChangeType option : - value or ChangeTypeOption::VALUE, default value of field, for example you can see hidden field type - allow_update, if you are not specify this option to true, then the edit page will not change this field value - target_dir, used for image and file to set directory to save the file in public directory - select_option, list of option for enum and select type of field, (*32)
hide the field, (*33)
$this->changeType('field_name', 'hidden', ['value' => 1]);
//or
$this->changeType('field_name', ChangeType::HIDDEN, [ChangeTypeOption::VALUE => 1]);
available option is 'value' and, (*34)
field type with number formated display, (*35)
$this->changeType('field_name', 'money');
$this->changeType('field_name', 'textarea');
dropdown/select field type, (*36)
$this->changeType('field_name', 'enum', ['select_option' => ['yes', 'no'] ]);
dropdown/select field type, (*37)
$option = array(
'0' => 'yes',
'1' => 'no'
);
$this->changeType('nama_field', 'enum', $option);
$this->changeType('field_name', 'enum', ['select_option' => ['yes', 'no'] ]);