Traits

Hush provides several traits, which you can attach to your models and simplify your life.

ScaryLayer\Hush\Traits\Imagable

Will simplify process of storing images.

All you need to configure it:

  1. Inherit that trait by your model.
  2. OPTIONAL: Specify the protected imagable property and assing array with field names to it. You model also has to contain fields with same names, where will be stored links to the images.
  3. OPTIONAL: Specify the protected imagable_multiple property and assing to it array with field names. In that case you should add (new {YourModelName})->createImagesTable() to your migrations and create imagable model by php artisan hush:imagable {YourModelName} artisan command.

Images will be stored, when you assing new value to your imagable field.
NOTE: For imagable_multiple you should do this only after saving the model. Also in that case your model will have automatically defined images relation.

Path of stored images will be:

/public/{config hush/app.php uploads_folder propery}/{model name in snake case}

Also trait provides some functions:

  • removeImage - available only for imagable_multiple fields, takes 2 params - imagable_multiple field name and image value, which should be deleted.
  • saveImage - which will just store uploaded file to disk without any additional logic.

ScaryLayer\Hush\Traits\Permissiable

Should be attached to model, which you want to give roles and permissions.
Model will have a new relation roleObject and method permitted which takes one param - permission name.

All you need to configure it:

  1. Inherit that trait by your model.
  2. Add field role for your model and migration.

ScaryLayer\Hush\Traits\Searchable

Will automatically add search queries when you run search() method on your query and request contains search param.

All you need to configure it:

  1. Inherit that trait by your model.
  2. Specify protected searchable property and assing array of field names, which have be checked by searcher.

ScaryLayer\Hush\Traits\Sortable

Will automatically add sort queries when you run sort() method on your eloquent query and request contains sort param.

All you need to configure it:

  1. Inherit that trait by your model.
  2. Specify protected sortable property and assing array of field names, which have to be available to sorter.

ScaryLayer\Hush\Traits\Translatable

Will simplify process of working with translations.
Will add translations relation to model and will return you already translated field value when you will try to get it.

All you need to configure it:

  1. Inherit that trait by your model.
  2. Specify the protected translatable property and assing to it array with field names.
  3. Add (new YourModelName)->createTranslationTable() to your migrations and create translatable model by php artisan hush:translatable {YourModelName} artisan command.

Also trait provide some functions:

  • saveTranslation which should be runned after saving the model, it will store the data for the given field name. Takes two params field and values which should be array with language_code => value pairs.
  • saveTranslations which should be runned after saving the model, it will store the data for all the translatable field names. You may use it, when you are lazy to store all fields by the saveTranslation method and want the trait to process all translatable fields by itself.
  • translate will translate the field value to given language. Takes two arguments field-name and language-code (if it is not specified, will translate field to app locale).
  • translationArray give you all translations for given field.