iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 21
0
Modern Web

用30days 了解web系列 第 21

(Day 21) 5 Laravel Helpers you should know

  • 分享至 

  • xImage
  •  

1. Logger
The logger helper function can be used to write a message with a debug level to the log.
to check the log message go to storage/log and the message should be look like the following
#0 [internal function]: Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(2, 'file_put_conten...', '/var/www/Blog...', 122, Array)

2. Dividing an array
The Arr::divide() method lets you split up an array in two arrays. The divide method returns two arrays. One array containing the keys and the other array containing the values.

use Illuminate\Support\Arr;
[$keys, $values] = Arr::divide(['name' => 'James', 'age' => 33]);
$keys: ['name', 'age']
$values: ['James', 33]

3. Blank
The blank helper function checks whether a value is “blank”. A “blank” value means null, a string only containing whitespaces or an empty array or string.
Note:
Booleans are not considered “blank” values.

blank('');
blank('   ');
blank(null);
blank(collect());
// Will result in: true
blank(0);
blank(true);
blank(false);
// Will result in: false

The inverse of this helper function is the filled helper function.

4. Dumping variables
Dumping variables is very handy if you want to debug one or more variables.

dump($variable);

5. Array has value
The Arr:has method can be used to check whether an item or multiple items exist in an array using the “dot” notation.
To check for multiple items simply pass an array instead of a string to the method.

use Illuminate\Support\Arr;
$blogs = ['blog' => ['title' => 'My blog', 'published' => true]];
$contains = Arr::has($blogs, 'blog.title'); 
// true
$contains = Arr::has($blogs, ['blog.title', 'blog.published']); 
// true
$contains = Arr::has($blogs, ['blog.title', 'blog.author']); 
// false

上一篇
(Day 20) Deploy your Laravel project to Docker
下一篇
(Day 22) cool Github repository should know
系列文
用30days 了解web30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言