2017 © Pedro Peláez
 

library crud

image

timenz/crud

  • Friday, June 23, 2017
  • by timenz
  • Repository
  • 1 Watchers
  • 2 Stars
  • 37 Installations
  • HTML
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Laravel Admin Crud

Laravel Controller extended, to easier create crud page., (*1)

Limitation and Requirement

  • Laravel 5.1
  • Mysql/Mariadb database
  • Bootstrap css framework
  • only 1 crud on one page

Install

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>

Mininal code example

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;
    }
}

Documentation

init

[wajib] Main init, (*12)

// parameter => table name, master blade and parameter for master blade
$this->init('users');

title

set crud title //parameter => crud title $this->setTitle('Article Crud');, (*13)

disAllow

Limit permission of crud, (*14)

//parameter L = list
$this->disAllow('LCREDSXO');

columns

Specify fields in listing at index page., (*15)

//parameter => array of fields 
$this->columns(array(
    'title',
));

validateRules

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',
));

fields

Specify fields at create, edit dan read page, (*17)

$this->fields(array(
    'title',
));

displayAs

Modify title of field, (*18)

$this->displayAs('title', 'The Title');

createFields

Specify fields at create page, (*19)

$this->createFields(array(
    'title',
));

editFields

Specify fields at edit page, (*20)

$this->editFields(array(
    'title',
));

readFields

Specify fields at read page, (*21)

$this->readFields(array(
    'title',
));

where

Filters visible data by using sql where, (*22)

//parameter => field, condition, value
$this->where('id', '=', 1);

setJoin

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');

orderBy

Set order listing data, (*24)

//parameter 1. field name, 2. sort (asc/desc)
$this->orderBy('id', 'asc');

callbackColumn

Set a custom column value in listing data, (*25)

$this->callbackColumn('field_name', function($row, $val){
    return number_format($val);
});

callbackBeforeSave

Provide some operation before saving data, (*26)

$this->callbackBeforeSave(function($post_data){

    $post_data['password'] = Hash::make($post_data['password']);
    return $post_data;
});

callbackBeforeUpdate

Provide some operation before update data, (*27)

$this->callbackBeforeUpdate(function($post_data){

    $post_data['password'] = Hash::make($post_data['password']);
    return $post_data;
});

addAction

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'));

changeType

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)

  1. Field name
  2. Change type
  3. Change type option in array

To avoid coding error, I have created two classes to specify available change type and change type option parameter., (*31)

ChangeTypeOption

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)

hidden

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)

money

field type with number formated display, (*35)

$this->changeType('field_name', 'money');

textarea

$this->changeType('field_name', 'textarea');

enum

dropdown/select field type, (*36)

$this->changeType('field_name', 'enum', ['select_option' => ['yes', 'no'] ]);

select

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'] ]);

The Versions

23/06 2017

dev-master

9999999-dev

  Sources   Download

The Requires

 

by Timen Chad

29/04 2016

dev-github

dev-github

  Sources   Download

The Requires

 

by Timen Chad