Helper Methods

Introduction

HotCoffee comes with some helper functions that can help speed up your development. You can also use the HotCoffee facade.

settings($key, $default)

Get a global setting value. string $key - Key of the setting. string $default - Default value, if key is not found.

<title> {{ settings('website_name') }} </title>
use TaffoVelikoff\HotCoffee\Facades\HotCoffee;

public function index() {
    $title = HotCoffee::settings('website_name');
    return view('home', compact('title'));
}

Get a menu and it's elements by keyword. string $keyword - Keyword of the menu. string $type - Type of the menu or blade template to use when rendering.

<!-- Get menu with key of 'main' and render as unstyled unordered list -->
{{ menu('main') }}

<!-- Get menu with key of 'main' and render using bootstrap -->
{{ menu('main', 'bootstrap') }}

<!-- Render using a custom view resources/views/main_menu.blade.php -->
{{ menu('main', 'main_menu') }}

is_json($string)

Check if a given string is in JSON format. string $string - String to check.

language_fields($fields, $edit)

Automatically generates HTML fields for the translatable attributes of a model. array $fields - HTML fields to be generated. mixed $edit - Model to be updated.

Wrap the language_fields() method around {!! !!} in your blade templates, instead of {{ }} to display the unescaped HTML output.

The above example will produce these HTML fields:

  • Input of type "text" with name "name" and label text "Name of product".

  • Textarea with name "description", label text "Description of product" and additional class "tinymce". Because of the hr => true a horizontal line will be added after the field to serve as a separator.

  • Textarea of 4 rows height, name "notes", label text "Additional Notes" and additional information text bellow the field "You can leave some additional notes here", colored in green (bootstrap success class is added to the text div).

If you want to change the template used to render the translatable fields you can copy vendor/taffovelikoff/hotcoffee/resources/views/admin/components/language_fields.blad.php to resources/views/vendor/hotcoffee/admin/components/language_fields.blad.php and edit as you desire.

sef_field($edit)

Generates field for the SEF keyword (for search engine friendly URLs). mixed $edit - Model to be updated.

Your model should use the TaffoVelikoff\LaravelSef\Traits\HasSef trait. You can create the custom URL by using $product->createSef('custom_url_name'); or update it with $product->updateSef('new_url');.

image_attachments_field($edit)

Generates an upload field for the image attachments. mixed $edit - Model to be updated.

Your model should use the Bnb\Laravel\Attachments\HasAttachment trait.

thumbnail($filepath, $dimensions, $fit, $source)

Dynamically generates a thumbnail for an image, stores it in the cache and renders it. string $filepath - Path to an image file. mixed $dimensions - String or array of dimensions. For example [300, 400] will create a thumbnail with width of 300px and height of 400px and '300' will create a square image of 300px. string $fit - Sets how the image is fitted to its target dimensions. Can be "contain", "max", "fill", "stretch" or "crop". mixed $source - Source directory to search for the image. By default it's the public folder.

Attachments have their own thumbnail() method. <img src="{{ $attachment->thumbnail([800, 600]) }}" />

language_validation_rules($ruleLine)

If you have used the {!! language_fields($fields) !!} helper method to generate the HTML fields for the translatable properties in your view you can also use language_validation_riles() to generate validation rules for all languages in your form requests or controllers. array $ruleLines Array of validation rules.

Now whenever add more locales you do not need to go back to the form request (or controller) and write validation more validation rules.

language_validation_messages($messageLines)

Generates validation messages for translatable fields for every languages defined in hotcoffee.php config file. It is very similar to language_validation_riles() but it applies to the custom validation messages. array $messageLines Array of custom validation messages.

Or even a cleaner way:

Last updated