[Feature proposal] Fluent syntax for CRUD
See original GitHub issueI think I just had an epiphany, because all these changes seem excellent to me AND I think they’re backwards-compatible:
(1) What if we use a Facade for the CRUD, and use CRUD::addField()
instead of $this->crud->addField()
? Shorter is better, right?
(2) What if we also create a fluent syntax for defining fields, columns, etc, like Laravel 5.4 does with its routes (Route::name('users.index')->middleware('auth');
)?
We could then use both this:
<?php namespace App\Http\Controllers\Admin;
use Backpack\CRUD\app\Http\Controllers\CrudController;
// VALIDATION: change the requests to match your own file names if you need form validation
use App\Http\Requests\ArticleRequest as StoreRequest;
use App\Http\Requests\ArticleRequest as UpdateRequest;
class ArticleCrudController extends CrudController {
public function setup() {
/*
|--------------------------------------------------------------------------
| BASIC CRUD INFORMATION
|--------------------------------------------------------------------------
*/
$this->crud->setModel("App\Models\Article");
$this->crud->setRoute("admin/article");
$this->crud->setEntityNameStrings('article', 'articles');
/*
|--------------------------------------------------------------------------
| COLUMNS AND FIELDS
|--------------------------------------------------------------------------
*/
// ------ CRUD COLUMNS
$this->crud->addColumn([
'name' => 'date',
'label' => 'Date',
'type' => 'date'
]);
$this->crud->addColumn([
'name' => 'status',
'label' => "Status"
]);
$this->crud->addColumn([
'name' => 'title',
'label' => "Title"
]);
$this->crud->addColumn([
'name' => 'featured',
'label' => "Featured",
'type' => "model_function",
'function_name' => 'getFeaturedColumn'
]);
$this->crud->addColumn([
'label' => "Category",
'type' => 'select',
'name' => 'category_id',
'entity' => 'category',
'attribute' => 'name',
'model' => "App\Models\Category"
]);
// ------ CRUD FIELDS
$this->crud->addField([ // TEXT
'name' => 'title',
'label' => 'Title',
'type' => 'text',
'placeholder' => 'Your title here'
]);
$this->crud->addField([
'name' => 'slug',
'label' => "Slug (URL)",
'type' => 'text',
'hint' => 'Will be automatically generated from your title, if left empty.'
// 'disabled' => 'disabled'
]);
$this->crud->addField([ // TEXT
'name' => 'date',
'label' => 'Date',
'type' => 'date',
'value' => date('Y-m-d')
], 'create');
$this->crud->addField([ // TEXT
'name' => 'date',
'label' => 'Date',
'type' => 'date'
], 'update');
$this->crud->addField([ // WYSIWYG
'name' => 'content',
'label' => 'Content',
'type' => 'ckeditor',
'placeholder' => 'Your textarea text here'
]);
$this->crud->addField([ // Image
'name' => 'image',
'label' => 'Image',
'type' => 'browse'
]);
$this->crud->addField([ // SELECT
'label' => "Category",
'type' => 'select2',
'name' => 'category_id',
'entity' => 'category',
'attribute' => 'name',
'model' => "App\Models\Category"
]);
$this->crud->addField([ // Select2Multiple = n-n
'label' => 'Tags',
'type' => 'select2_multiple',
'name' => 'tags',
'entity' => 'tags',
'attribute' => 'name',
'model' => "App\Models\Tag",
'pivot' => true,
]);
$this->crud->addField([ // ENUM
'name' => 'status',
'label' => "Status",
'type' => 'enum'
]);
$this->crud->addField([ // CHECKBOX
'name' => 'featured',
'label' => "Featured item",
'type' => 'checkbox'
]);
}
public function store(StoreRequest $request)
{
return parent::storeCrud();
}
public function update(UpdateRequest $request)
{
return parent::updateCrud();
}
}
AND this:
<?php namespace App\Http\Controllers\Admin;
use Backpack\CRUD\app\Http\Controllers\CrudController;
// VALIDATION: change the requests to match your own file names if you need form validation
use App\Http\Requests\ArticleRequest as StoreRequest;
use App\Http\Requests\ArticleRequest as UpdateRequest;
class ArticleCrudController extends CrudController {
public function setup() {
/*
|--------------------------------------------------------------------------
| BASIC CRUD INFORMATION
|--------------------------------------------------------------------------
*/
CRUD::setModel("App\Models\Article");
CRUD::setRoute("admin/article");
CRUD::setEntityNameStrings('article', 'articles');
/*
|--------------------------------------------------------------------------
| COLUMNS AND FIELDS
|--------------------------------------------------------------------------
*/
// ------ CRUD COLUMNS
CRUD::addColumn('date')->label('Date')->type('date');
CRUD::addColumn('status')->label('Status');
CRUD::addColumn('name')->label('Title');
CRUD::addColumn('featured')->label('Featured')->type('model_function')->function_name('getFeaturedColumn');
CRUD::addColumn('category_id')->type('select')->label('Category')->entity('category')->attribute('name')->model('App\Models\Category');
// ------ CRUD FIELDS
CRUD::addField('title')->label('Title')->type('text')->placeholder('Your title here');
CRUD::addField('slug')->label('Slug (URL)')->type('text')->hint('Will be automatically generated from your title, if left empty');
CRUD::addField('date')->label('Date')->type('date')->value(date('Y-m-d'))->showOnCreate(); // alternatives: showOnCreate(); showOnUpdate(); showOnBoth(); hideOnCreate(); hideOnUpdate();
CRUD::addField('date')->label('Date')->type('date')->showOnUpdate();
CRUD::addField('content')->label('Content')->type('ckeditor')->placeholder('Your textarea text here');
CRUD::addField('image')->label('Image')->type('browse');
CRUD::addField('category_id')->type('select2')->label('Category')->entity('category')->attribute('name')->model('App\Models\Category');
CRUD::addField('tags')->type('select2_multiple')->label('Tags')->entity('tags')->attribute('name')->model('App\Models\Tag')->pivot(true);
CRUD::addField('name')->label('Status')->type('enum');
CRUD::addField('featured')->label('Featured item')->type('checkbox');
}
public function store(StoreRequest $request)
{
return parent::storeCrud();
}
public function update(UpdateRequest $request)
{
return parent::updateCrud();
}
}
I’m super-excited about this, because
- it looks and works more Laravely
- it would help us use the IDE’s autocomplete more
- I personally like it better, because it makes for shorter controllers
but I don’t exactly have strong technical reasons… Thoughts?
Issue Analytics
- State:
- Created 7 years ago
- Reactions:3
- Comments:24 (24 by maintainers)
Top Results From Across the Web
CRUD Operations Using Entity Framework 5.0 Code First ...
In this article you will learn how to perform CRUD operations using the Entity Framework 5.0 Code First approach in MVC.
Read more >How to Make a CRUD App with Entity Framework Core
Learn how to build a basic CRUD application using Entity Framework Core.
Read more >Operations :: 5.x Docs - Backpack for Laravel
When creating a CRUD Panel, your EntityCrudController (where Entity = your model name) is extending CrudController . By default, no operations are enabled....
Read more >Relation queries (Concepts) - Prisma
The fluent API lets you fluently traverse the relations of your models via function calls. Note that the last function call determines the...
Read more >XAF - 2022 Roadmap (Cross-Platform .NET App UI & Web API ...
Release of the FREE Web API Service for Basic CRUD & Authorization ... NET 6, XAF v22.1 will support fluent API syntax in...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Started implementing this in #826 but waiting for some feedback for a while now.
really? then it’s perfect
does it accept a single string like this? haven’t checked