Laravel5 tips
helpers:
logger() 或者 Log门面Illuminate\Log\Logger
你可以使用 Log
门面记录日志信息,如上所述,日志系统提供了定义在 RFC 5424 规范中的八种日志级别:emergency、alert、critical、error、warning、 notice、info 和 debug:
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
/**
* 显示指定用户的属性
*
* @param int $id
* @return Response
*/
public function showProfile($id)
{
Log::info('Showing user profile for user: '.$id);
Log::info('User failed to login.', ['id' => $user->id]);
//有时候你可能希望将日志信息记录到某个通道而不是应用的默认通道。要实现这个目的,你可//以使用 Log 门面上的 channel 方法来获取配置文件定义的通道并将日志写入进去:
Log::channel('slack')->info('Something happened!');
//如果你想要创建一个由多个通道组成的按需日志堆栈,可以使用 stack 方法:
Log::stack(['single', 'slack'])->info('Something happened!');
return view('user.profile', ['user' => User::findOrFail($id)]);
}
}
Log::emergency($error);
Log::alert($error);
Log::critical($error);
Log::error($error);
Log::warning($error);
Log::notice($error);
Log::info($error);
Log::debug($error);
The logger helper function can be used to write a message with a debug level to the log.
logger('Product was changed.', ['id' => $product->id]);
Which results in adding the following line to the log:
[2019-06-14 09:53:22] local.DEBUG: Product was changed. {"id":4}
If you don’t pass any value to the logger function, it will return a Logger instance. This allows you to write messages of different levels to the log.
logger()->error('An error occurred');
This adds the following line to the log:
[2019-06-14 09:56:12] local.ERROR: An error occurred
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]
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.
Path helpers:
These are the helper functions that Laravel has when it comes to paths:
app_path()
base_path()
config_path()
database_path()
public_path()
resource_path()
storage_path()
echo storage_path();
// Output:
"C:\Path\To\My\Project\storage"
You can also pass an argument to the path helper functions, which will get appended to the path:
echo storage_path('attachment.pdf');
// Output:
"C:\Path\To\My\Project\storage\attachment.pdf"
Str::slug()
$slug = Str::slug('Laravel Is Awesome');
$slug: "laravel-is-awesome"
The default separator is a hyphen (-), but you can overwrite this by passing a second argument to the function.
$slug = Str::slug('Laravel Is Awesome', '&');
$slug: "laravel&is&awesome"
Arr:has()
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
Str::uuid()
use Illuminate\Support\Str;
echo(string) Str::uuid(); // "2ad4abcc-8adc-47b6-b21e-9e5497a8af1b"
Arr::pluck()
The Arr::pluck method retrieves all of the values for a given key from an array.
$parents = [
['parent' => ['id' => 1, 'name' => 'James']],
['parent' => ['id' => 8, 'name' => 'Lisa']],
];
Arr::pluck($parents, 'parent.name'); // ['James', 'Lisa']