laravel常用辅助函数

数组

use Illuminate\Support\Arr;

Arr::collapse([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
Arr::flatten(['name' => 'Joe', 'languages' => ['PHP', 'Ruby']]);  // 将多维数组中数组的值取出平铺为一维数组 ['Joe', 'PHP', 'Ruby']
[$keys, $values] = Arr::divide(['name' => 'Desk']); //$keys: ['name']  $values: ['Desk']

Arr::except(['name' => 'Desk', 'price' => 100], ['price']);  //['name' => 'Desk']
Arr::only(['name' => 'Desk', 'price' => 100, 'orders' => 10], ['name', 'price']); // ['name' => 'Desk', 'price' => 100]
Arr::first([100, 200, 300], function ($value, $key) {  // 200  Arr::first($array, $callback, $default);
    return $value >= 150;
});
Arr::last([100, 200, 300, 110], function ($value, $key) { // 300  Arr::last($array, $callback, $default);
    return $value >= 150;
});
Arr::where([100, '200', 300, '400', 500], function ($value, $key) {  // [1 => '200', 3 => '400']
    return is_string($value);
});
Arr::whereNotNull([0, null]); // [0 => 0]
Arr::pluck([  // ['Taylor', 'Abigail']
    ['developer' => ['id' => 1, 'name' => 'Taylor']],
    ['developer' => ['id' => 2, 'name' => 'Abigail']],
], 'developer.name');
Arr::pluck([  // [1 => 'Taylor', 2 => 'Abigail']
    ['developer' => ['id' => 1, 'name' => 'Taylor']],
    ['developer' => ['id' => 2, 'name' => 'Abigail']],
], 'developer.name', 'developer.id');
Arr::forget(['products' => ['desk' => ['price' => 100]]], 'products.desk');  //  ['products' => []]

Arr::get(['products' => ['desk' => ['price' => 100]]], 'products.desk.price'); // 100
Arr::get(['products' => ['desk' => ['price' => 100]]], 'products.desk.discount', 0); // 0
Arr::has(['product' => ['name' => 'Desk', 'price' => 100]], 'product.name'); // true
Arr::has(['product' => ['name' => 'Desk', 'price' => 100]], ['product.price', 'product.discount']); // false
Arr::hasAny(['product' => ['name' => 'Desk', 'price' => 100]], ['product.price', 'product.discount']); // true

Arr::query([  // name=Taylor&order[column]=created_at&order[direction]=desc
    'name' => 'Taylor',
    'order' => [
        'column' => 'created_at',
        'direction' => 'desc'
    ]
]);
Arr::random([1, 2, 3, 4, 5]); //随机取一个
Arr::random([1, 2, 3, 4, 5], 2); //随机取2个  [2, 5]
Arr::dot(['products' => ['desk' => ['price' => 100]]]); //['products.desk.price' => 100]
Arr::undot([ // ['user' => ['name' => 'Kevin Malone', 'occupation' => 'Accountant']]
    'user.name' => 'Kevin Malone',
    'user.occupation' => 'Accountant',
]);

data_fill(['products' => ['desk' => ['price' => 100]]], 'products.desk.price', 200); // ['products' => ['desk' => ['price' => 100]]]
data_fill(['products' => ['desk' => ['price' => 100]]], 'products.desk.discount', 10); // ['products' => ['desk' => ['price' => 100, 'discount' => 10]]]
data_fill([
    'products' => [
        ['name' => 'Desk 1', 'price' => 100],
        ['name' => 'Desk 2'],
    ],
], 'products.*.price', 200);
data_get(['products' => ['desk' => ['price' => 100]]], 'products.desk.price');  // 100
data_get(['products' => ['desk' => ['price' => 100]]], 'products.desk.discount', 0); // 0
data_get([ // ['Desk 1', 'Desk 2'];
    'product-one' => ['name' => 'Desk 1', 'price' => 100],
    'product-two' => ['name' => 'Desk 2', 'price' => 150],
], '*.name');
data_set(['products' => ['desk' => ['price' => 100]]], 'products.desk.price', 200); // ['products' => ['desk' => ['price' => 200]]]
data_set([
    'products' => [
        ['name' => 'Desk 1', 'price' => 100],
        ['name' => 'Desk 2', 'price' => 150],
    ],
], 'products.*.price', 200);
data_set(['products' => ['desk' => ['price' => 100]]], 'products.desk.price', 200, $overwrite = false); // ['products' => ['desk' => ['price' => 100]]]

 

 

字符串

use Illuminate\Support\Str;

e('<html>foo</html>');  // &lt;html&gt;foo&lt;/html&gt;
preg_replace_array('/:[a-z_]+/', ['8:30', '9:00'], 'between :start and :end'); //between 8:30 and 9:00

Str::after('This is my name', 'This is'); // ' my name'
Str::afterLast('App\Http\Controllers\Controller', '\\'); // 'Controller'
Str::before('This is my name', 'my name');  // 'This is '
Str::beforeLast('This is my name', 'is'); // 'This '
Str::between('This is my name', 'This', 'name'); // ' is my '

Str::contains('This is my name', 'my'); // true
Str::contains('This is my name', ['my', 'foo']); // true
Str::containsAll('This is my name', ['my', 'name']); // true
Str::startsWith('This is my name', 'This'); // true
Str::startsWith('This is my name', ['This', 'That', 'There']); // true
Str::endsWith('This is my name', 'name'); // true
Str::endsWith('This is my name', ['name', 'foo']); // true
Str::endsWith('This is my name', ['this', 'foo']); // false
//用来判断字符串是否与指定模式匹配
Str::is('foo*', 'foobar'); // true
Str::is('baz*', 'foobar'); // false

Str::studly('foo_bar'); // FooBar
Str::camel('foo_bar'); // fooBar
Str::snake('fooBar'); // foo_bar
Str::snake('fooBar', '-'); // foo-bar
Str::kebab('fooBar'); // foo-bar
Str::excerpt('This is my name', 'my', [
    'radius' => 3
]); // '...is my na...'  截断字符串
Str::excerpt('This is my name', 'name', [
    'radius' => 3,
    'omission' => '(...) '
]); // '(...) my name'
Str::limit('The quick brown fox jumps over the lazy dog', 20); // The quick brown fox...
Str::limit('The quick brown fox jumps over the lazy dog', 20, ' (...)'); // The quick brown fox (...)
//将指定的字符串修改为以指定的值结尾的形式
Str::finish('this/string', '/'); // this/string/  
Str::finish('this/string/', '/'); // this/string/
//将给定的值添加到字符串的开始位置
Str::start('this/string', '/'); // /this/string
Str::start('/this/string', '/'); // /this/string
//将由大小写、连字符或下划线分隔的字符串转换为空格分隔的字符串,同时保证每个单词的首字母大写
Str::headline('steve_jobs'); // Steve Jobs
Str::headline('EmailNotificationSent'); // Email Notification Sent


Str::ascii('û'); // 'u'  尝试将字符串转换为 ASCII 值
Str::isAscii('Taylor'); // true
Str::isAscii('ü'); // false
Str::isUuid('a0a2a2d2-0b87-4a18-83f2-2529882be2de'); // true
Str::markdown('# Laravel'); // <h1>Laravel</h1>
Str::markdown('# Taylor <b>Otwell</b>', [
    'html_input' => 'strip',
]); // <h1>Taylor Otwell</h1>
//重复的字符掩盖字符串的一部分
Str::mask('taylor@example.com', '*', 3); // tay***************
Str::mask('taylor@example.com', '*', -15, 3); // tay***@example.com

Str::padBoth('James', 10, '_'); // '__James___'
Str::padBoth('James', 10); // '  James   '
Str::padLeft('James', 10, '-='); // '-=-=-James'
Str::padLeft('James', 10); // '     James'
Str::padRight('James', 10, '-'); // 'James-----'
Str::padRight('James', 10); // 'James     '
Str::plural('car'); // cars
Str::plural('child'); // children
Str::singular('cars'); // car
Str::remove('e', 'Peter Piper picked a peck of pickled peppers.'); // Ptr Pipr pickd a pck of pickld ppprs.
Str::replace('8.x', '9.x', 'Laravel 8.x'); // Laravel 9.x
Str::replaceArray('?', ['8:30', '9:00'], 'The event will take place between ? and ?'); // The event will take place between 8:30 and 9:00
Str::replaceFirst('the', 'a', 'the quick brown fox jumps over the lazy dog'); // a quick brown fox jumps over the lazy dog
Str::replaceLast('the', 'a', 'the quick brown fox jumps over the lazy dog'); // the quick brown fox jumps over a lazy dog
Str::reverse('Hello World'); // dlroW olleH
Str::slug('Laravel 5 Framework', '-'); // laravel-5-framework 将给定的字符串生成一个 URL 友好的「slug」
Str::title('a nice title uses the correct case'); // A Nice Title Uses The Correct Case

Str::substrCount('If you like ice cream, you will like snow cones.', 'like'); // 2
Str::wordCount('Hello, world!'); // 2
Str::words('Perfectly balanced, as all things should be.', 3, ' >>>'); // Perfectly balanced, as >>>
Str::uuid();

Str::of('Taylor')
    ->when(true, function ($string) {
        return $string->append(' Otwell');
    }); // 'Taylor Otwell'
Str::of('tony stark')
    ->whenContains('tony', function ($string) {
        return $string->title();
    }); // 'Tony Stark'
Str::of('tony stark')
    ->whenContains(['tony', 'hulk'], function ($string) {
        return $string->title();
    }); // Tony Stark
Str::of('tony stark')
    ->whenContainsAll(['tony', 'stark'], function ($string) {
        return $string->title();
    }); // Tony Stark
Str::of('  ')->whenEmpty(function ($string) {
    return $string->trim()->prepend('Laravel');
}); // 'Laravel'
Str::of('Framework')->whenNotEmpty(function ($string) {
    return $string->prepend('Laravel ');
}); // 'Laravel Framework'
Str::of('disney world')->whenStartsWith('disney', function ($string) {
    return $string->title();
}); // 'Disney World'
Str::of('disney world')->whenEndsWith('world', function ($string) {
    return $string->title();
}); // 'Disney World'
Str::of('laravel')->whenExactly('laravel', function ($string) {
    return $string->title();
}); // 'Laravel'
Str::of('foo/bar')->whenIs('foo/*', function ($string) {
    return $string->append('/baz');
}); // 'foo/bar/baz'
Str::of('foo/bar')->whenIsAscii('laravel', function ($string) {
    return $string->title();
});// 'Laravel'

 

其它

//路径
public_path();
public_path('css/app.css');
resource_path();
resource_path('sass/app.scss');
storage_path();
storage_path('app/file.txt');

//url
action([UserController::class, 'profile'], ['id' => 1]);
asset('img/photo.jpg');
route('route.name');
route('route.name', ['id' => 1]);
url('user/profile', [1]);

auth()->user();
bcrypt('my-secret-password');
encrypt('my-secret-value');
decrypt($value);

blank('');
blank('   ');
blank(null);
blank(collect());
// true
blank(0);
blank(true);
blank(false);
// false
filled(0);
filled(true);
filled(false);
// true
filled('');
filled('   ');
filled(null);
filled(collect());
// false

cache('key');
cache('key', 'default');
cache(['key' => 'value'], 300);
cache(['key' => 'value'], now()->addSeconds(10));

collect(['taylor', 'abigail']);

config('app.timezone');
config('app.timezone', $default);
env('APP_ENV');
env('APP_ENV', 'production');

dispatch(new App\Jobs\SendEmails); //将给定的 任务 推送到 Laravel 任务队列
event(new UserRegistered($user)); //向监听器派发给定 事件

$request = request();
$value = request('key', $default);
return response('Hello World', 200, $headers);
return response()->json(['foo' => 'bar'], 200, $headers);

$value = session('key');
$value = session()->get('key');
session()->put('key', $value);
session(['chairs' => 7, 'instruments' => 3]);

$today = today(); //根据当前日期创建新的 Illuminate\Support\Carbon 实例
$now = now(); //为当前时间创建一个新的 Illuminate\Support\Carbon 实例

 

posted @ 2023-05-12 19:23  carol2014  阅读(300)  评论(0编辑  收藏  举报