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.
1
<title> {{ settings('website_name') }} </title>
Copied!
1
use TaffoVelikoff\HotCoffee\Facades\HotCoffee;
2
3
public function index() {
4
$title = HotCoffee::settings('website_name');
5
return view('home', compact('title'));
6
}
Copied!
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.
1
<!-- Get menu with key of 'main' and render as unstyled unordered list -->
2
{{ menu('main') }}
3
4
<!-- Get menu with key of 'main' and render using bootstrap -->
5
{{ menu('main', 'bootstrap') }}
6
7
<!-- Render using a custom view resources/views/main_menu.blade.php -->
8
{{ menu('main', 'main_menu') }}
Copied!
1
use TaffoVelikoff\HotCoffee\Facades\HotCoffee;
2
...
3
4
// Get menu items as collection
5
$menuItems = HotCoffee::menu('main', 'collection');
6
7
// Get as a json string
8
$menuItems = HotCoffee::menu('main', 'json');
Copied!

is_json($string)

Check if a given string is in JSON format. string $string - String to check.
1
is_json('{ "name":"John" }'); // returns true
2
is_json('is this json?'); // returns false
Copied!

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.
1
{!! language_fields([
2
'name' => [
3
'type' => 'text',
4
'title' => 'Name of product'
5
],
6
'description' => [
7
'type' => 'textarea',
8
'title' => 'Description of product',
9
'class' => 'tinymce',
10
'hr' => true
11
],
12
'notes' => [
13
'type' => 'textarea',
14
'title' => 'Additional Notes',
15
'info' => [
16
'type' => 'success',
17
'content' => 'You can leave some additional notes here.'
18
],
19
'rows' => '4'
20
]
21
], $product ?? null) !!}
Copied!
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.
1
{!! sef_field($product ?? null) !!}
Copied!
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.
1
{!! image_attachments_field($product ?? null) !!}
Copied!
Your model should use the Bnb\Laravel\Attachments\HasAttachment trait.
1
// Attach the images
2
foreach($request->file('images') as $file) {
3
$product->attach($file, ['group' => 'images']);
4
}
5
6
// Get image attachments
7
$attachments = $product->attachment('images');
Copied!

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

Be careful when/how you use this. It can slow down page loading time.
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.
1
<!-- Thumbnail for public/images/logo.png with dimensions of 400x400 -->
2
<img src="{{ thumbnail('images/logo.png', '400') }}" />
3
4
<!-- Thumbnail for public/images/logo.png with dimensions of 500x400 and crop -->
5
<img src="{{ thumbnail('images/logo.png', '400', 'crop') }}" />
Copied!
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.
1
$rules = language_validation_rules(
2
'title' => 'required|min:3',
3
'content' => 'required',
4
);
5
6
// Given that we have 4 locales in hotcoffee.php (en, bg, fr and de) will return
7
[
8
'title.en.title' => 'required|min:3',
9
'title.bg.title' => 'required|min:3',
10
'title.de.title' => 'required|min:3',
11
'title.fr.title' => 'required|min:3',
12
'content.en.title' => 'required',
13
'content.bg.title' => 'required',
14
'content.de.title' => 'required',
15
'content.fr.title' => 'required',
16
];
Copied!
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.
1
/**
2
* Example form request using language_validation_rules()
3
* and language_validation_messages() helper methods.
4
*/
5
6
namespace App\Http\Requests\Admin;
7
8
use Illuminate\Validation\Rule;
9
use Illuminate\Foundation\Http\FormRequest;
10
11
class StoreProduct extends FormRequest
12
{
13
/**
14
* Determine if the user is authorized to make this request.
15
*
16
* @return bool
17
*/
18
public function authorize()
19
{
20
return true;
21
}
22
23
/**
24
* Get the validation rules that apply to the request.
25
*
26
* @return array
27
*/
28
public function rules()
29
{
30
31
// For non-translatable (or normal) fields
32
$normal = [
33
'price' => 'numeric',
34
];
35
36
// For translatable fields
37
$translatable = language_validation_rules([
38
'title' => 'required|max:64|min:3',
39
'content' => 'required',
40
]);
41
42
// Merge all
43
return array_merge($normal, $translatable);
44
45
}
46
47
/**
48
* Get the error messages for the defined validation rules.
49
*
50
* @return array
51
*/
52
public function messages()
53
{
54
55
// For non-translatable (or normal) fields
56
$normal = [
57
'price.numeric' => __('admin.err_price_not_numeric'),
58
];
59
60
// For translatable fields
61
$translatable = language_validation_messages([
62
'title.required' => 'admin.err_title_required',
63
'title.max' => 'admin.err_title_max',
64
'title.min' => 'admin.err_title_min',
65
'content.required' => 'admin.err_content_required',
66
]);
67
68
// Merge all
69
return array_merge($normal, $translatable);
70
}
71
}
Copied!
Or even a cleaner way:
1
public function rules()
2
{
3
return array_merge(
4
[
5
'price' => 'numeric'
6
],
7
language_validation_rules([
8
'title' => 'required|max:64|min:3',
9
'content' => 'required',
10
])
11
);
12
}
Copied!
Last modified 1yr ago