0 Star 0 Fork 0

IT大叔 / laravel-admin-plus

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
model-form.md 7.26 KB
一键复制 编辑 原始数据 按行查看 历史
leno 提交于 2017-06-23 15:23 . 首次上传

Model-Form

The Encore\Admin\Form class is used to generate a data model-based form. For example, there is a movies table in the database

CREATE TABLE `movies` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `director` int(10) unsigned NOT NULL,
  `describe` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `rate` tinyint unsigned NOT NULL,
  `released` enum(0, 1),
  `release_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

The corresponding data model is App\Models\Movie, and the following code can generate the movies data form:


use App\Models\Movie;
use Encore\Admin\Form;
use Encore\Admin\Facades\Admin;

$grid = Admin::form(Movie::class, function(Form $grid){

    // Displays the record id
    $form->display('id', 'ID');

    // Add an input box of type text
    $form->text('title', 'Movie title');
    
    $directors = [
        'John'  => 1,
        'Smith' => 2,
        'Kate'  => 3,
    ];
    
    $form->select('director', 'Director')->options($directors);
    
    // Add textarea for the describe field
    $form->textarea('describe', 'Describe');
    
    // Number input
    $form->number('rate', 'Rate');
    
    // Add a switch field
    $form->switch('released', 'Released?');
    
    // Add a date and time selection box
    $form->dateTime('release_at', 'release time');
    
    // Display two time column 
    $form->display('created_at', 'Created time');
    $form->display('updated_at', 'Updated time');
});

// Displays the form content
echo $form;

Basic Usage

Text input

$form->text($column, [$label]);

// Add a submission validation rule
$form->text($column, [$label])->rules('required|min:10');

select

$form->select($column[, $label])->options([1 => 'foo', 2 => 'bar', 'val' => 'Option name']);

Or load option by ajax

$form->select($column[, $label])->options('/admin/demo/options');

The json format returned from url /admin/demo/options:

[
    {
        "id": 1,
        "text": "hello"
    },
    {
        "id": 2,
        "text": "world"
    },
]

Multiple select

$form->multipleSelect($column[, $label])->options([1 => 'foo', 2 => 'bar', 'val' => 'Option name']);

Or load option by ajax

$form->multipleSelect($column[, $label])->options('/admin/demo/options');

The json format returned from url /admin/demo/options:

[
    {
        "id": 1,
        "text": "hello"
    },
    {
        "id": 2,
        "text": "world"
    },
]

textarea:

$form->textarea($column[, $label]);

radio

$form->radio($column[, $label])->values(['m' => 'Female', 'f'=> 'Male'])->default('m');

checkbox

The values () method is used to set options:

$form->checkbox($column[, $label])->values([1 => 'foo', 2 => 'bar', 'val' => 'Option name']);

email input

$form->email($column[, $label]);

password input

$form->password($column[, $label]);

url input

$form->url($column[, $label]);

ip input

$form->ip($column[, $label]);

phone number input

$form->mobile($column[, $label])->format('999 9999 9999');

color select

$form->color($column[, $label])->default('#ccc');

time select

$form->time($column[, $label]);

// Set the time format, more formats reference http://momentjs.com/docs/#/displaying/format/    
$form->time($column[, $label])->format('HH:mm:ss');

date input

$form->date($column[, $label]);

// 设置日期格式,更多格式参考http://momentjs.com/docs/#/displaying/format/
$form->date($column[, $label])->format('YYYY-MM-DD');

datetime input

$form->datetime($column[, $label]);

// Set the date format, more format reference http://momentjs.com/docs/#/displaying/format/
$form->datetime($column[, $label])->format('YYYY-MM-DD HH:mm:ss');

time range select

$startTime$endTimeis the start and end time fields:

$form->timeRange($startTime, $endTime, 'Time Range');

date range select

$startDate$endDateis the start and end date fields:

$form->dateRange($startDate, $endDate, 'Date Range');

datetime range select

$startDateTime$endDateTime is the start and end datetime fields:

$form->datetimeRange($startDateTime, $endDateTime, 'DateTime Range');

currency input

$form->currency($column[, $label]);

// set the unit symbol
$form->currency($column[, $label])->symbol('¥');

number input

$form->number($column[, $label]);

rate input

$form->rate($column[, $label]);

image upload

You can use compression, crop, add watermarks and other methods, please refer to [[Intervention] (http://image.intervention.io/getting_started/introduction)], picture upload directory in the file config / admin.php Upload.image configuration, if the directory does not exist, you need to create the directory and open write permissions:

$form->image($column[, $label]);

// Modify the image upload path and file name
$form->image($column[, $label])->move($dir, $name);

// Crop picture
$form->image($column[, $label])->crop(int $width, int $height, [int $x, int $y]);

// Add a watermark
$form->image($column[, $label])->insert($watermark, 'center');

file upload

The file upload directory is configured in upload.file in the file config/admin.php. If the directory does not exist, it needs to be created and write-enabled.

$form->file($column[, $label]);

// Modify the file upload path and file name
$form->file($column[, $label])->move($dir, $name);

// And set the upload file type
$form->file($column[, $label])->rules('mimes:doc,docx,xlsx');

map

Used to select the latitude and longitude, $ latitude, $ longitude for the latitude and longitude field, using Tencent map when locale set of laravel is zh_CN, otherwise use Google Maps:

$form->map($latitude, $longitude, $label);

// Use Tencent map
$form->map($latitude, $longitude, $label)->useTencentMap();

// Use google map
$form->map($latitude, $longitude, $label)->useGoogleMap();

slide

Can be used to select the type of digital fields, such as age:

$form->slider($column[, $label])->options(['max' => 100, 'min' => 1, 'step' => 1, 'postfix' => 'years old']);

rich text editor

$form->editor($column[, $label]);

json editor

$form->json($column[, $label]);

hidden field

$form->hidden($column);

switch

On and off pairs of switches with the values 1 and 0:

$form->switch($column[, $label])->states(['on' => 1, 'off' => 0]);

display field

Only display the fields and without any action:

$form->display($column[, $label]);

divide

$form->divide();

saving callback

Add callback function when saving data, can use it do some operations when saving data.

$form->saving(function(Form $form) {
    if($form->password && $form->model()->password != $form->password)
    {
        $form->password = bcrypt($form->password);
    }
});
PHP
1
https://gitee.com/itdashu/laravel-admin-plus.git
git@gitee.com:itdashu/laravel-admin-plus.git
itdashu
laravel-admin-plus
laravel-admin-plus
master

搜索帮助