Entire CRUD system in as little as three lines.
In your model:
use Illuminate\Database\Eloquent\Model;
use ctbuh\Admin\CrudResourceTrait;
class User extends Model
{
use CrudResourceTrait; // required
public function fields($context){
// optional - will be automatically generated based on Schema.
}
}
in your controller:
use App\Http\Controllers\Controller;
use ctbuh\Admin\Traits\HasCrudActions;
class UserController extends Controller
{
use HasCrudActions; // required
protected $resource = "App\Models\User"; // required
}
is managed by either:
Grid::fromResource("App\Models\Package");
Form::edit("App\Models\Package", 45);
-
Resource/Data Model/Model
- interchangeable and all refer to same thing which is almost always an instance to Eloquent model.
-
Field/Column
- are the same thing -> depending on the context on term will be used.
-
Context
- which "CRUD state" is the form currently in. E.g index, store, create, etc...
You will mostly be dealing with these properties which are all public
name
- required, and will never be empty. Can be attribute name,label
- Will default to reverse snakecaseresource
- model model to which this field belongs to
Methods:
-
setValue($data_model)
-
formatValueUsing($callback)
- This will extract the value from the data model that initially fills.
- Defaults to $data_model->{$field->name};
-
formatGridValueUsing($callback)
Defined inside fields() method.
Text::make($attribute_name, $label)
Is dealing with basic attributes.
Text::make('first_name');
Text::make('last_name', 'Surname');
Text::make('email');
label is optional. Value will resolve to the attribute name that belongs to that model.
options
Appear as either a dropdown list, a radio button list, or a checkbox list when multiple=true
multiple($boolean)
setOptions($data)
- either an array or Eloquent instance that will be queried.placeholder($text)
- will add an additional OPTION with value=null, and text=$textsetOptionsUsing($callback|string)
queryOptionsUsing($builder)
- called if $data is an eloquent Model
TODO:
asEnum()
asDropdownList()
-- defaultasRadioList()
asCheckboxList()
-- default when multiple=true
Examples
Select::make('salutation')->setOptions(array(
'Mr.' => 'Mr.',
'Mrs.' => 'Mrs.',
'Ms.' => 'Ms.'
))->placeholder('Select title... ');
Select::make('language')->multiple(true)->setOptions(array(
'en' => 'English',
'cn' => 'Chinese',
));
// resolve on demand
Select::make('country_id', 'Select Country')->setOptions(Country::class);
Select::make('country_id', 'Country')->getOptions("App\Models\Country");
index/show:
Either a plain string or JSON string if multiple=true
$resource->{$field->name}
store/update:
// 'Mr.'
$resource->salutation = request()->get('salutation');
when multiple=true
// "{['en', 'cn']}"
$resource->language = json_encode(request()->get('language'));
Most commonly used with relations.
BelongsTo::make('user_id', 'Select User')->setOptionsUsing(function($data){
return $data->pluck('name', 'id'); // default
});
BelongsTo::make('registrant_id')->queryOptionsUsing(function($builder){
$builder->select('id', 'email')->whereNull('group_id');
})->setOptionsUsing('id:email'); // value:text
// TODO: convert to Select2
BelongsTo::make('person_id')->ajax('/api/people');
getValue()
$resource->user_id
gridValue():
$resource->user()->label()
$resource->user_id
Submit Logic:
$resource->user_id = request()->get('user_id');
BelongsToMany::make('topics');
// TODO:
BelongsToMake::make('companies')->withPivotFields(function($resource){
return array(
Text::make('company_name'),
Text::make('company_name_chinese')
)
});
Will parse out the apropriate model to query from TOPICS relationship.
value():
$resource->topics | Collection
index / gridValue():
$resource->topics[].label
Context: store/update:
$resource->topics->sync(request()->get('topic_id'))
// TODO: pivotSyncing
Introduces three new fields.
withFields($closure)
getRelatedFields()
getRelatedResource()
examples:
HasMany::make('keywords');
HasMany::make('keywords')->withFields(function($data){
return array(
Text::make('keyword')
);
});
create/edit:
Not visible on 'create'. model has to be created first, before those can be edited.
Always Collection
:
$resource->keywords
index:
$resource->keywords[].label
too much duplicate code. have to reefine columns for boht eit/update bootstrap specific too much
nova: vue-based
https://github.com/nsaumini/nova-field-count/blob/master/src/RelationshipCount.php