This command generates the crud (model, controller, migration, form, views, seed) for a particular model.
php artisan administrable:make:crud {Model}
The –migrate or -m option is used to run artisan migrate command
php artisan administrable:make:crud {Model} --migrate="true" # or --migrate="false"
The default value is true
To customize the generation, a configuration file administrable.yaml located at the root of the project is used. This file uses the Yaml language. If you do not know this syntax you can go to the official website to learn more. Je vous assure ce n’est pas tres compliqué à apprendre.
Le fichier de définition
Le fichier de personnalisation est composé de deux blocs, le block globals
qui permet de définir des actions qui affecteront tous les crud qui seront generes plus tard.
globals:
# Where to store models in app folder
folder: Models
# activate or deactivate slug editing in form globally
edit_slug: false
# Permet d'ajouter un bouton clone sur la page index pour accélerer la creation
# de nouveaux eleemts en se basant sur un element.
clone: true
# use fillable or guarded. You can not use both fillable and guarded property
fillable: true # guarded: false
# Cette option permet de générer un seeder pour populer le modele.
# Elle accepte un boolean ou un integer qui sera le nombre d'eléments à enregistrer en bdd.
# Si c'est la valeur est `true` alors le nombre sera 15 par défaut.
seeder: true
Le second block est models
qui sera la partie reserver à la définition des futurs modèles à générer
Example of model declaration: Post
Models:
Post:
breadcrumb: name
imagemanager: true
fields:
- name: { name: name, type: string, rules: nullable, slug: true }
- image: { name: image, type: image, rules: required }
Tous les champs doivent etre declares dans la partie fields
Définition d’un modèle
Nous allons voir toutes les options et possibilités disponibles pour définir un modele. Je vous conseille de voir toutes les options avant de vous lancer.
Table name
To change database table name
Models:
Post:
table_name: posts
Seeder
To avoid generating a seeder file
Models:
Post:
seeder: false
The default value is true
Timestamps
By default for each model laravel manages the created_at and updated_at fields. To deactivate it you can proceed as follows:
Models:
Post:
timestamps: false
It will add on the model the attribute
protected $timestamps = false;
The default value is true
Imagemanger
Pour utiliser le gestionnaire de fichier pour lier des images sur le modele.
To use the image manager for a specific model.
A model can only use 03 collections (front, back and images)
To use all collections and default labels
Models:
Post:
imagemanager: true
You can choose the collection(s) to use
Models:
Post:
imagemanager:
- front
- back
- images
Labels can be changed this way
Models:
Post:
imagemanager: { front: front image label, back: back image label, images: images label }
or
Models:
Post:
imagemanager:
front: front image label
back: back image label
images: images label
Icon
To change the icon used in the administration sidebar.
Models:
Post:
icon: fa-folder
The icon must be taken from the font awesome library and the default value is fa-folder
Generate
il faudra retirer la partie entité car maintenant ont peut passer l’option generate pour choisir le ou les fichiers qu’on souhaite générer.
Note:
generate: model|migration|seed|form|controller|route|view
ou
Note:
generate: model,migration,seed,form,controller,route,view
ou
Note:
generate:
- model
- migration
- seed
- form
- controller
- route
- view
Actions
Sometimes we do not want to generate all crud actions (index, show, create, edit, delete).
Actions can be separated by a , (comma)
Post:
actions: index,show,create,edit,delete
Actions can be separated by the character pipe |
Post:
actions: index|show|create|edit|delete
Actions can be declared as a list
Post:
actions:
- index
- show
- create
- edit
- delete
The authorized values are: index, show, create, edit and delete
Slug
To sluggify a field and use slug instead of id for routes
Models:
Post:
fields:
- name: { slug: true }
or pass the slug option at the model level with the name of the field to be sluggified which must be defined beforehand (important).
Models:
Post:
slug: name
fields:
- name: { name: name, type: string:125, rules: nullable }
- age: { type: int:3 }
NB:
- one of the two options can be used.
- only one field can be sluggable
- only (text, longText, mediumText) fields can be sluggify
Edit slug
To have the slug field in form and edit it
Models:
Post:
edit_slug: true
fields:
- name: { slug: true }
Or use it globally. This will affect all models
globals:
edit_slug: true
The default value is false and slug field is required on the model
Clone
To have a button in index view to clone or duplicate a field
Models:
Post:
clone: true
Or use it globally. This will affect all models
globals:
clone: true
The default value is true
Fillable
To use fillable or guarded property in the model
Models:
Post:
fillable: true
Avec cette syntaxe tous les champs seront fillable. Si vous voulez customiser les gens à filler
Models:
Post:
fillable: name|age
fields:
- name: { slug: true }
- age: { type: integer }
ou
Post:
fillable:
- name
- age
fields:
- name: { slug: true }
- age: { type: integer }
Or use it globally. This will affect all models
globals:
fillable: true
The default value is true
Guarded
To use guarded property in the model
Models:
Post:
guarded: true
Avec cette syntaxe tous les champs seront fillable. Si vous voulez customiser les gens à filler
Models:
Post:
guarded: name|age
fields:
- name: { slug: true }
- age: { type: integer }
ou
Models:
Post:
guarded:
- name
- age
fields:
- name: { slug: true }
- age: { type: integer }
Or use it globally. This will affect all models
globals:
guarded: false
The default value is false
You can not use both fillable and guarded properties
Subfolder
Si vous voulez palcer les fichiers generes dans un sous dossier pour mieux organiser les fichiers.
Models:
Post:
subfolder: shop
Dans cet exemple tous les fichiers seront generes dans leurs dossiers specifiques et particulierement dans le sous dossier.
Nom
Models:
Post:
fields:
- name: { name: name }
Elle peut etre optionnel et ce sera la clé du champ qui sera utilise.
Type
Models:
Post:
fields:
- name: { type: string }
Available types are: string, text, mediumText, longText, date, datetime, boolean, enum, decimal, float, double, integer, mediumInteger, bigInteger, polymorphic, ipAdress, image or file, relation, file
The default type is: string
Form
You can pass valid html5 attributes to the form that will be generated
Models:
Post:
fields:
- online: { type: boolean, form: { id: online, class: form-control } }
supported attributes are id, class , pattern, type, readonly, disabled
Choices
On peut passer l’option choice a un champ de type bool pour gerer ce qui va s’afficher dans le formulaire
Models:
Post:
fields:
- online: { type: boolean, trans: en ligne, choices: { choice1: choice1, choice2: choice2 } }
Cast
Attribute casting allow you to transform Eloquent attribute values when you retrieve or set them on models instances.
Models:
User:
fields:
- age: { type: integer, cast: integer }
boolean, integer and datetime types are automatically cast
Datepicker
Add datepicker for date type fields.
Models:
Event:
fields:
- start_at: { type: datetime, datepicker: true }
only date and datetime fields can have a datepicker.
Daterange
For a date range type field, two fields will be created to represent the start field and the end field respectively with the suffix start_at and end_at. Ces valeurs peuvent etre change dans le fichier de configuration.
Models:
Product:
fields:
- promotion: { type: datetime, daterange: true }
only date or datetime fields can have a datepicker
Validation
Models:
Post:
fields:
- title: { rules: required }
It uses the default validation rules of laravel validation.
Several rules must be separated by the pipe | character
Models:
Post:
fields:
- title: { rules: required|string|min:2 }
for required rule we can use the diminutive req (just an alias)
Models:
Post:
fields:
- title: { rules: req|string|min:2 }
Nullable
To make a field nullable
Models:
Post:
fields:
- title: { nullable: true }
or use the nullable validation rule
Models:
Post:
fields:
- title: { rules: nullable }
If a field has the nullable validation rule, the field is automatically nullable. If a field is nullable then it automatically has the nullable validation rule. No need to specify it twice.
Length
To define the length of a field
Models:
Post:
fields:
- name: { length: 60 }
For the string (text, mediumText, longText) or int (int, smallInt, BigInt) types, you can pass the size of the field to the type separate by :
Models:
Post:
fields:
- name: { type: string:60 }
Models:
User:
fields:
- age: { type: int:18 }
Translation
The model can be translated with the trans option
Models:
Post:
trans: article
fields:
- title: { name: title, type: string:125, rules: nullable }
A field can be translated too.
Models:
Post:
fields:
- name: { trans: nom }
For date range field, you must separate the translation field with the | (pipe) character.
Models:
Post:
fields:
- promotion: { type: datetime, daterange: true, trans: promotion start | promotion end }
This translation is only used for displaying in views. If the option is not passed, the translation file in resources/lang in the current locale folder will be used.
Default value
Set default value
Models:
Post:
fields:
- name: { default: john }
Tinymce
To use the tinymce rich editor for a field
Models:
Post:
fields:
- name: { type: text, tinymce: true }
Only available on text fields
Constraints
Models:
Post:
fields:
- title: { constraints: nullable }
plusieurs contraintes peuvent etre séparées par un | ou ,
Models:
Post:
fields:
- name: { constraints: nullable|unique|index } # or { constraints: nullable,unique,index }
or
Models:
Post:
fields:
- name:
constraints:
- nullable
- unique
- index
NB:
- The constraints must be valid according to the database management system.
- For the constraint set null, no need to pass the field to nullable. It’s done automatically.
- On the other hand, do not forget to remove the validation rule required if it is present because a field cannot be both required and nullable.
- If the field is nullable no need to put it in the list of constraints and vice versa.
Breadcrumb
For showing breadcrumb in views, the procedure is as follows:
Models:
Post:
fields:
- age: { breadcrumb: true }
or pass the breadcrumb option at the model level with the name of the field to use
Models:
Post:
breadcrumb: age
fields:
- age: { type: integer }
Default value is true
NB
- only one of the two possibilities can be used
- only one field can have this breadcrumb attribute
- the breadcrumb is only used in administration panel
Modèle polymorphic
Le modele polymorphique consiste a geneerr un modele et une migration avec des champs polymorphic Lorsqu’on passe polymorphic true a um modele seul le modele et la migration sont generessi le morphname est null on utilise le nom du champ auquel on ajoute able si ce n’est pas le cas
Models:
Post:
polymorphic: true
fields:
- url : { name: url, type: string }
- image: { type: polymorphic, morphname: imageable }
Relationships
Type simple
One to One
Nous allons partir du principe que nous sommes en train de generere un modele note (Note) et qu’il doit etre relie a un utilisateur (User)
Models:
Post:
fields:
- user_id: { type: { relation: one to one, type: simple, related: App\Models\User, property: name }, rules: required }
The related is optional, we guest the name by remove the _id on the field’s name. If the field doest not respect this convention, you have to use this option. pour les relations et pour l’attribut related si pas fourni et que le modele a un subfolder le related est devine en fonvtion de ce modele cad le related sera chercher dans ce dossier si ce n’est pas le cas il faudra passer le related soit avec le nom de la classe ou le namespace complet
Models:
Post:
fields:
- user_id: { type: relation }
In this case, the related will be User.
It is possible to use the full qualify model class name App\Models\User.
Models:
Post:
fields:
- user_id: { type: relation, related: App\Models\User }
You can change the key used for the foreign key.
Models:
Post:
fields:
- user_id: { type: { relation: one to one, type: simple, related: user, property: name, references: id }, rules: required }
By default id is used
You can change the deletion policy.
Models:
Post:
fields:
- user_id: { type: { relation: one to one, type: simple, related: user, property: name, onDelete: cascade }, rules: required }
Pour les relations de type haseOne et hasMany, la regle de validation exists sera automatiquement ajouté dans ce sera
exists:users,id
. Le champ id peut etre change avec l’attribute references.
NB:
- the authorized values are: cascade and set null.
- no onDelete on a polymorphic type field.
- if a field must be polymorphic it will be necessary to create the linked model first before making the connection
- for relationship type fields if the related is not passed it is the name of the field which is used
Foreign keys
dabord il faudra que les cles soient les memes des deux cotés
Models:
Post:
fields:
- user_id:
type:
relation: one to one
type: simple
related: App\Models\User
property: name
related_keys: { foreign_key: user_id }
local_keys: { foreign_key: user_id }
rules: required
La cle local_keys
correspond au modele qui existe deja et qui subit la relation. Dans notre cas c’est le modele User et qui aura la methode hasOne
ou hasMany
local_keys: {}
ensuite passé la cle etrangere
local_keys: { foreign_key: user_id }
Pour changer la cle locale
local_keys: { foreign_key: user_id, local_key: id }
pour le modele en train d’etre creer dans notre cas le modele Note et qui doit avoir la methode belongsTo
utiliser le
related_keys: { foreign_key: user_id }
si le parent na pas de cchamp id pour la liaison utilisez le local key
related_keys: { owner_key: id }
One to Many
Nous allons partir du principe que nous sommes en train de generere un modele note (Note) et qu’il doit etre relie a un utilisateur (User)
Post:
fields:
- user_id: { type: { relation: one to many, type: simple, related: App\Models\User, property: name }, rules: required }
NB:
- The related is obligatory we must know to which field the relation is linked
- The related is mandatory and is the linked model. We can just pass the name or the namespace.
- The property is the field used only for display and must exist on the linked model.
The related is optional, we guest the name by remove the _id on the field’s name. If the field doest not respect this convention, you have to use this option. pour les relations et pour l’attribut related si pas fourni et que le modele a un subfolder le related est devine en fonvtion de ce modele cad le related sera chercher dans ce dossier si ce n’est pas le cas il faudra passer le related soit avec le nom de la classe ou le namespace complet
Models:
Post:
fields:
- user_id: { type: { relation: one to many, type: simple, related: App\Models\User, property: name }, rules: required }
In this case, the related will be User.
It is possible to use the full qualify model class name App\Models\User.
Models:
Post:
fields:
- user_id: { type: relation, related: App\Models\User }
You can change the key used for the foreign key.
Models:
Post:
fields:
- user_id: { type: { relation: one to one, type: simple, related: user, property: name, references: id }, rules: required }
By default id is used
You can change the deletion policy.
Models:
Post:
fields:
- user_id: { type: { relation: one to one, type: simple, related: user, property: name, onDelete: cascade }, rules: required }
Pour les relations de type haseOne et hasMany, la regle de validation exists sera automatiquement ajouté dans ce sera
exists:users,id
. Le champ id peut etre change avec l’attribute references.
NB:
- the authorized values are: cascade and set null.
- no onDelete on a polymorphic type field.
- if a field must be polymorphic it will be necessary to create the linked model first before making the connection
- for relationship type fields if the related is not passed it is the name of the field which is used
Foreign keys
dabord il faudra que les cles soient les memes des deux cotés
Post:
fields:
- user_id:
type:
relation: one to many
type: simple
related: App\Models\User
property: name
related_keys: { foreign_key: user_id }
local_keys: { foreign_key: user_id }
rules: required
La cle local_keys
correspond au modele qui existe deja et qui subit la relation. Dans notre cas c’est le modele User et qui aura la methode hasOne
ou hasMany
local_keys: {}
ensuite passé la cle etrangere
local_keys: { foreign_key: user_id }
Pour changer la cle locale
local_keys: { foreign_key: user_id, local_key: id }
pour le modele en train d’etre creer dans notre cas le modele Note et qui doit avoir la methode belongsTo
utiliser le
related_keys: { foreign_key: user_id }
si le parent na pas de cchamp id pour la liaison utilisez le local key
related_keys: { owner_key: id }
Many to Many (simple)
Nous allons partir du principe que nous sommes en train de generer un modele note Note et qui sera lié avec un utilisateur User.
Note:
subfolder: shop
fields:
- user_id:
type:
relation: many to many
type: simple
related: App\Models\User
property: name
rules: required
NB:
- the pivot table is automatically generated.
- you can change the name of the pivot intermediate table.
La cle related_keys
correspond a la methode du related modele que nous allons ajouter au modele que nous allons créer. Dans notre cas c’est le modele Note
Note:
subfolder: shop
fields:
- user_id:
type:
relation: many to many
type: simple
related: App\Models\User
property: name
related_keys: { join_key: user_id, foreign_key: note_id }
rules: required
Dans cette situation foreign_key
correspond a la cle etrange du modele que nous sommes en train de generer qui est Note
Dans cette situation join_key
correspond a la cle etrange du modele existant et qui suit la relation dans notre cas c est User
La cle local_keys
correspond au modele qui existe deja et qui subit la relation et que nous allons ajouter dans le modele que nous sommes en train de generer. Dans notre cas c’est le modele User
Note:
subfolder: shop
fields:
- user_id:
type:
relation: many to many
type: simple
related: App\Models\User
property: name
local_keys: { foreign_key: note_id, join_key: user_id }
rules: required
Dans cette situation join_key
correspond a la cle etrange modele existant et qui suit la relation dans notre cas c est User
Dans cette situation foreign_key
correspond a la cle etrange du modele que nous sommes en train de generer qui est Note
Polymorphic type
One to One (polymorphic)
Le related model doit exister et contenir la methode morphTo.
Models:
Post:
fields:
- user_id:
type:
relation: one to one
type: polymorphic
related: user # or App\Models\User
property: name
rules: required
One to Many (polymorphic)
Le related model doit exister et contenir la methode morphTo.
Post:
user_id:
type:
relation: one to many
type: polymorphic
related: user
property: name
rules: required
Mettre le related explanation ici
Many to Many polymorphic
Le related model doit exister et contenir la methode morphTo.
Par default le morphname est le nom du modele suivi de able. vous pouvez changer le morphname en passant morphname: taggable
Post:
tag:
type:
relation: many to many
type: polymorphic
related: tag
property: name
rules: required