Laravel - Packages - Backpack

# Introduction

Laravel Backpack 的文件翻譯筆記


# Questions and Answers

以下的 Laravel Backpack Field example code 的意思是?
  • Example:

    <?php
    [
    'prefix' => '',
    'suffix' => '',
    'default' => 'some value', // set a default value
    'hint' => 'Some hint text', // helpful text, shows up after the input
    'attributes' => [
    'placeholder' => 'Some text when empty',
    'class' => 'form-control some-class',
    'readonly'=>'readonly',
    'disabled'=>'disabled',
    ], // change the HTML attributes of your input
    'wrapperAttributes' => [
    'class' => 'form-group col-md-12'
    ], // change the HTML attributes for the field wrapper - mostly for resizing fields
    ]
  • Answer:

    <?php
    [
    'prefix' => '',
    'suffix' => '',
    'default' => 'some value', // set a default value
    'hint' => 'Some hint text', // helpful text, shows up after the input
    'attributes' => [
    'placeholder' => 'Some text when empty',
    'class' => 'form-control some-class',
    'readonly'=>'readonly',
    'disabled'=>'disabled',
    ], // change the HTML attributes of your input
    'wrapperAttributes' => [
    'class' => 'form-group col-md-12'
    ], // change the HTML attributes for the field wrapper - mostly for resizing fields
    ]
以下的 Laravel Backpack Field example code 的意思是?
  • Example:

    <?php
    [
    'name' => 'description',
    'type' => 'textarea',
    'label' => 'Article Description',
    ]
  • Answer:
    name: 對應到資料庫中的欄位
    type: input type
    label: human readable 的 input 顯示

以下的 Laravel Backpack example code 的意思是?
  • Example:

    <?php
    php artisan backpack:crud tag

    php artisan backpack:base:add-custom-route "CRUD::resource('tag', 'TagCrudController');"

    php artisan backpack:base:add-sidebar-content "<li><a href='{{ backpack_url('tag') }}'><i class='fa fa-tag'></i> <span>Tags</span></a></li>"
  • Answer:
    建立 Tag model, Tag request, 以及 TagCrudController
    建立一個 resource route 到 routes/backpack/custom.php 檔案中 (位於 admin prefix, auth middleware), url 為 tag, 目標 controller 為 TagCrudController
    產生 doll sidebar, 並定義其內容

以下的 Laravel Backpack example code 的意思是?
  • Example:

    <?php
    <?php namespace App\Http\Controllers\Admin;

    use Backpack\CRUD\app\Http\Controllers\CrudController;

    use App\Http\Requests\TagCrudRequest as StoreRequest;
    use App\Http\Requests\TagCrudRequest as UpdateRequest;

    class TagCrudController extends CrudController {

    public function setup()
    {
    $this->crud->setModel("App\Models\Tag");
    $this->crud->setRoute("admin/tag");
    $this->crud->setEntityNameStrings('tag', 'tags');

    $this->crud->setColumns(['name', 'slug']);
    $this->crud->addField([
    'name' => 'name',
    'type' => 'text',
    'label' => "Tag name"
    ]);
    $this->crud->addField([
    'name' => 'slug',
    'type' => 'text',
    'label' => "URL Segment (slug)"
    ]);
    }

    public function store(StoreRequest $request)
    {
    return parent::storeCrud();
    }

    public function update(UpdateRequest $request)
    {
    return parent::updateCrud();
    }
    }
  • Answer:

    <?php
    <?php namespace App\Http\Controllers\Admin;

    use Backpack\CRUD\app\Http\Controllers\CrudController;

    use App\Http\Requests\TagCrudRequest as StoreRequest;
    use App\Http\Requests\TagCrudRequest as UpdateRequest;

    class TagCrudController extends CrudController {

    public function setup()
    {
    // 設定使用的 model
    $this->crud->setModel("App\Models\Tag");
    // 設定 route, 位於 routes/custom.php
    $this->crud->setRoute("admin/tag");
    // 設定 entity 單複數, 供 Backpack 套件內部各模組使用
    $this->crud->setEntityNameStrings('tag', 'tags');

    // 設定 GET 可得到的 column
    $this->crud->setColumns(['name', 'slug']);
    // 設定前端顯示的 field
    // name: 對應到的 db 中的 column
    // type: filed kind, 像是 text, number, select2
    // label: human readable input name, 如果不指定, 預設為 name
    $this->crud->addField([
    'name' => 'name',
    'type' => 'text',
    'label' => "Tag name"
    ]);
    $this->crud->addField([
    'name' => 'slug',
    'type' => 'text',
    'label' => "URL Segment (slug)"
    ]);
    }

    // 若在 form 驗證沒過, 到不了這
    public function store(StoreRequest $request)
    {
    return parent::storeCrud();
    }

    public function update(UpdateRequest $request)
    {
    return parent::updateCrud();
    }
    }
以下的 Laravel Backpack example code 的意思是?
  • Example:

    <?php
    // relation
    public function articles()
    {
    return $this->belongsToMany('App\Models\Article', 'article_tag');
    }

    // In CrudController
    $this->crud->addField([
    'type' => 'select2_multiple',
    'name' => 'articles',
    'entity' => 'articles',
    'attribute' => 'title',
    'pivot' => true,
    ], 'update');
  • Answer:
    type: 定義 field, 類型為可多選 input
    name: 該 model 的 relation name
    entity: 該 model 的 relation name
    attribute: Article model 中, 要顯示在 select input 的欄位
    pivot: 是否 cascade delete & update
    update: 指定該 filed 只會顯示在 update form, 而非 create form, 若不帶, 則兩邊都會出現

以下的 Laravel Backpack example code 的意思是?
  • Example:

    <?php
    $this->crud->setFromDb()
  • Answer:
    Backpack 會根據 DB column 的 type 自己決定 input type, 只適用於簡單的應用場景

以下的 Laravel Backpack example code 的意思是?
  • Example:

    <?php
    $this->crud->addColumn($column_definition_array);
    $this->crud->addColumns([$column_definition_array_1, $column_definition_array_2]);
    $this->crud->removeColumn('column_name');
    $this->crud->removeColumns(['column_name_1', 'column_name_2']);
    $this->crud->setColumnDetails('column_name', ['attribute' => 'value']);
    $this->crud->setColumnsDetails(['column_1', 'column_2'], ['attribute' => 'value']);

    $this->crud->setColumns([$column_definition_array_1, $column_definition_array_2]);
  • Answer:

    <?php
    // 可以定義 column 要用什麼方式回傳, 可以是 date, array
    // 增加一個指定 column 回傳值
    $this->crud->addColumn($column_definition_array);
    // 增加多個指定 column 回傳值
    $this->crud->addColumns([$column_definition_array_1, $column_definition_array_2]);
    // 移除一個指定 column 回傳值
    $this->crud->removeColumn('column_name'); // remove a column from the table
    // 移除多個指定 column 回傳值
    $this->crud->removeColumns(['column_name_1', 'column_name_2']);
    // 設定單個 column, 以及其 detail attribute, 只允許回傳該 column
    $this->crud->setColumnDetails('column_name', ['attribute' => 'value']);
    // 設定多個 column, 以及其 detail attribute, 只允許回傳這些 column
    $this->crud->setColumnsDetails(['column_1', 'column_2'], ['attribute' => 'value']);

    // 設定多個 columns, 只允許回傳這些 columns
    $this->crud->setColumns([$column_definition_array_1, $column_definition_array_2]);
Git - Template Laravel - Eloquent ORM - Serialization (官方文件原子化翻譯)

留言

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×