Loading

Laravel 命名转换函数总览表格

📊 核心函数总览表

 
函数 输入示例 输出示例 命名风格 主要用途 辅助函数别名
Str::studly() hello_world
user-profile
first name
HelloWorld
UserProfile
FirstName
大驼峰
(PascalCase)
类名、模型名、控制器名 studly_case()
Str::camel() hello_world
UserProfile
first-name
helloWorld
userProfile
firstName
小驼峰
(camelCase)
变量名、方法名、属性名 camel_case()
Str::snake() HelloWorld
userProfile
FirstName
hello_world
user_profile
first_name
蛇形
(snake_case)
数据库字段、配置键名、环境变量 snake_case()
Str::kebab() HelloWorld
userProfile
first_name
hello-world
user-profile
first-name
烤串
(kebab-case)
URL路由、CSS类名、HTML属性 kebab_case()
Str::slug() Hello World!
Laravel 8.x
用户资料
hello-world
laravel-8x
用户资料
URL友好
(Slug)
URL片段、SEO链接、文件名 str_slug()
Str::plural() user
person
category
users
people
categories
复数形式 表名、资源路由、消息本地化 -
Str::singular() users
people
categories
user
person
category
单数形式 模型名、关系方法名 -
Str::title() hello world
user_profile
Hello World
User_Profile
标题大小写
(Title Case)
文章标题、显示名称、页面标题 title_case()
Str::ucfirst() hello
hello world
Hello
Hello world
首字母大写 句子开头、名称首字母 ucfirst()
Str::upper() hello
Hello
HELLO
HELLO
全大写 常量、配置键、环境变量 strtoupper()
Str::lower() HELLO
Hello
hello
hello
全小写 统一格式、比较、存储 strtolower()
Str::ucsplit() HelloWorld
XMLHttpRequest
['Hello', 'World']
['XML', 'Http', 'Request']
拆分大写单词 分析类名、字符串解析 -
Str::finish() path/to
domain.com
path/to/
domain.com/
添加后缀 路径处理、URL标准化 -
Str::start() to/path
example.com
/to/path
https://example.com
添加前缀 路径处理、URL标准化 -

🎯 详细用法说明

1. Str::studly() - 大驼峰命名

// 用法:转换分隔符连接的字符串为大驼峰
use Illuminate\Support\Str;

// 基本用法
Str::studly('user_profile');           // UserProfile
Str::studly('user-profile');           // UserProfile
Str::studly('first name last name');   // FirstNameLastName

// 实际应用:动态类名
$table = 'user_profiles';
$modelClass = 'App\\Models\\' . Str::studly(Str::singular($table));
// App\Models\UserProfile

2. Str::camel() - 小驼峰命名

// 用法:转换分隔符连接的字符串为小驼峰
Str::camel('user_profile');            // userProfile
Str::camel('User-Profile');            // userProfile

// 实际应用:API响应字段转换
$user = User::find(1);
$response = [
    'id' => $user->id,
    'userName' => Str::camel($user->user_name),
    'emailAddress' => Str::camel($user->email_address),
];

3. Str::snake() - 蛇形命名

// 用法:转换驼峰字符串为蛇形命名,可指定分隔符
Str::snake('HelloWorld');              // hello_world
Str::snake('userProfile');             // user_profile
Str::snake('FirstName', '-');          // first-name (指定分隔符)

// 实际应用:JSON数据转数据库字段
$jsonData = ['userName' => 'John', 'emailAddress' => 'test@example.com'];
$dbData = [];
foreach ($jsonData as $key => $value) {
    $dbData[Str::snake($key)] = $value;
}
// ['user_name' => 'John', 'email_address' => 'test@example.com']

4. Str::kebab() - 烤串命名

// 用法:转换字符串为kebab-case(小写+连字符)
Str::kebab('HelloWorld');              // hello-world
Str::kebab('userProfile');             // user-profile
Str::kebab('first_name');              // first-name

// 实际应用:生成路由
$controller = 'UserProfileController';
$routeName = Str::kebab(str_replace('Controller', '', $controller));
// user-profile
Route::get('/' . $routeName, [$controller, 'index']);

5. Str::slug() - URL友好字符串

// 用法:生成URL友好的slug,移除特殊字符
Str::slug('Hello World!');             // hello-world
Str::slug('Laravel 8.x 发布');          // laravel-8x-发布
Str::slug('User@Example.com');         // userexamplecom

// 指定分隔符和语言
Str::slug('Hello World', '_');         // hello_world
Str::slug('Café au lait', '-', 'fr');  // cafe-au-lait

// 实际应用:博客文章URL
$title = 'How to Use Laravel Collections';
$slug = Str::slug($title); // how-to-use-laravel-collections

6. Str::plural() / Str::singular() - 单复数转换

// 用法:智能单复数转换
Str::plural('user');                   // users
Str::plural('person');                 // people
Str::plural('UserProfile');            // UserProfiles

Str::singular('users');                // user
Str::singular('people');               // person

// 带数量判断
Str::plural('user', 1);                // user
Str::plural('user', 2);                // users

// 实际应用:表名转模型名
$table = 'user_profiles';
$model = Str::singular(Str::studly($table)); // UserProfile

7. Str::title() - 标题大小写

// 用法:每个单词首字母大写
Str::title('hello world');             // Hello World
Str::title('user_profile');            // User_Profile (保留下划线)

// 实际应用:页面标题显示
$pageTitle = Str::title(str_replace('-', ' ', $slug));
// how-to-use-laravel → How To Use Laravel

8. Str::ucfirst() - 首字母大写

// 用法:字符串首字母大写
Str::ucfirst('hello');                 // Hello
Str::ucfirst('hello world');           // Hello world

// 实际应用:用户名称格式化
$name = 'john doe';
$formatted = Str::ucfirst($name);      // John doe

9. Str::upper() / Str::lower() - 大小写转换

// 用法:全大写/全小写转换
Str::upper('hello');                   // HELLO
Str::lower('HELLO');                   // hello

// 实际应用:统一比较
if (Str::lower($input) === 'yes') {
    // 忽略大小写比较
}

10. Str::ucsplit() - 拆分大写单词

// 用法:按大写字母拆分字符串
Str::ucsplit('HelloWorld');            // ['Hello', 'World']
Str::ucsplit('XMLHttpRequest');        // ['XML', 'Http', 'Request']

// 实际应用:分析类名结构
$parts = Str::ucsplit('UserProfileController');
// ['User', 'Profile', 'Controller']

⚠️ 重要注意事项

  1. 辅助函数别名在 Laravel 9+ 中已废弃,建议使用 Str:: 门面

  2. Str::snake() 处理全大写字符串时,会将每个字母拆开

  3. Str::slug() 会移除特殊字符,而 Str::kebab() 会保留

  4. 单复数转换支持常见的不规则变化,但可能不完全准确

posted @ 2026-01-15 15:37  Carvers  阅读(8)  评论(0)    收藏  举报