• Laravel 常用辅助函数


    Laravel 常用辅助函数

    辅助函数

    Laravel 包含各种全局辅助函数。
    laravel 中包含大量辅助函数,您可以使用它们来简化开发工作流程。

    array_dot() 辅助函数允许你将多维数组转换为使用点符号的一维数组

    $array = [
        'user' => ['username' => 'something'],
        'app' => ['creator' => ['name' => 'someone'], 'created' => 'today']
    ];
    
    $dot_array = array_dot($array);
    
    // [user.username] => something, [app.creator.name] => someone, [app.created] => today
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

    array_get() 函数使用点符号从多维数组中检索值

    $array = [
        'user' => ['username' => 'something'],
        'app' => ['creator' => ['name' => 'someone'], 'created' => 'today']
    ];
    
    $name = array_get($array, 'app.creator.name');
    
    // someone
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    如果 key 不存在,array_get() 函数还接受可选的第三个参数作为默认值。

    $name = array_get($array, 'app.created.name', 'anonymous');
    
    // anonymous
    
    • 1
    • 2
    • 3

    public_path() 返回 Laravel 应用程序中公共目录的完全限定的绝对路径

    还可以将路径传递到公共目录中的文件或目录以获取该资源的绝对路径。
    它将简单地将 public_path() 添加到你的参数中。

    $public_path = public_path();
    
    $path = public_path('js/app.js');
    
    • 1
    • 2
    • 3

    Str::orderedUuid() 函数首先生成一个时间戳 uuid

    这个uuid可以存储在索引数据库列中。

    这些uuid是基于时间戳创建的,因此它们会保留你的内容索引。

    在Laravel 5.6中使用它时,
    会引发Ramsey\Uuid\Exception\UnsatisfiedDependencyException
    要解决此问题,只需运行以下命令即可使用 moontoast/math 包:

    composer require "moontoast/math"
    
    • 1
    use Illuminate\Support\Str;
    
    return (string) Str::orderByUuid()
    
    // A timestamp first uuid
    
    • 1
    • 2
    • 3
    • 4
    • 5

    str_plural 函数将字符串转换为复数形式,该功能只支持英文

    echo str_plural('bank');
    
    // banks
    
    echo str_plural('developer');
    
    // developers
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    route() 函数为指定的路由生成路由URL

    $url = route('login');
    
    • 1

    如果路由接受参数,你可以简单地将它们作为第二个参数传递给一个数组。

    $url = route('products', ['id' => 1]);
    
    • 1

    如果你想产生一个相对的URL而不是一个绝对的URL,你可以传递 false 作为第三个参数。

    $url = route('products', ['id' => 1], false);
    
    • 1

    tap() 函数接受两个参数:一个值和一个闭包

    该值将被传递给闭包,然后该值将被返回。
    闭包返回值无关紧要。

    $user = App\User::find(1);
    
    return tap($user, function($user) {
        $user->update([
            'name' => 'Random'
        ]);
    });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    它不会返回布尔值,而是返回 User Model

    如果你没有传递闭包,你也可以使用 User Model 的任何方法。

    无论实际返回的方法如何,返回值都将始终为值。

    在下面的例子中,它将返回 User Model 而不是布尔值。
    update 方法返回布尔值,但由于用了 tap ,所以它将返回 User Model

    $user = App\User::find(1);
    
    return tap($user)->update([
        'name' => 'SomeName'
    ]);
    
    • 1
    • 2
    • 3
    • 4
    • 5

    optional() 函数接受一个参数,你可以调用参数的方法或访问属性

    如果传递的对象为 null,则方法和属性将返回 null,而不是导致错误或抛出异常。

    $user = User::find(1);
    
    return optional($user)->name;
    
    • 1
    • 2
    • 3

    集合

    一维数组去重 duplicates

    duplicates 方法可以检索出集合中所有重复的值。返回的数组中会包含每个值在原数组中的键。

    
    $collection = collect(['james', 'lisa', 'ryan', 'james', 'brad', 'lisa']);
    $collection->duplicates(); // [3 => 'james', 5 => 'lisa']
    
    • 1
    • 2
    • 3

    遍历集合 each

    在不使用 foreach 的情况下也可以遍历集合,就是使用 each 方法。像通常的 foreach 方法一样, each 方法也有两个参数,分别是 itemkey

    
    $collection->each(function ($item, $key) {
        // Do stuff
    });
    
    • 1
    • 2
    • 3
    • 4

    集合 has 方法可以用来查看某个键是否存在于集合中

    参数可以是字符串,也可以是数组。

    如果传了数组作为参数,那么数组中所有的值都必须是集合中的键,结果才能返回 true,否则返回 false

    
    $collection = collect([
      'title' => 'Harry Potter', 
      'author' => 'J.K. Rowling', 
      'price' => 25
      ]);
      
    $collection->has('author'); // true
    $collection->has(['title', 'price']); // true
    $collection->has(['price', 'rating']); // false
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    集合 implode 方法可以连接集合中的值

    这个方法的作用跟 PHP 中的 implode 方法非常像。

    
    
    $collection = collect([
        ['title' => 'Gift card', 'price' => 50],
        ['title' => 'Chair', 'price' => 80],
    ]);
    $collection->implode('title', ', ');
    // Gift card, Chair
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    如果集合中的值只是字符串,或数字,你只需要传递连接符作为参数即可。

    
    $collection = collect([1,2,'foo',3,'bar']);
    $collection->implode('-');
    // 1-2-foo-3-bar
    
    • 1
    • 2
    • 3
    • 4

    集合 Push 与 Pull

    可以使用 push 方法将项目附加到集合的末尾。
    如果需要添加到集合的开头,则可以使用 prepend 方法。

    
    
    $collection = collect([1, 2, 3]);
    $collection->push(4);
    $collection->all(); // [1, 2, 3, 4]
    
    • 1
    • 2
    • 3
    • 4
    • 5

    pull 方法将给定键从集合中移除并将返回其对应的值。

    
    
    $collection = collect([
        'title' => 'Harry Potter',
        'author' => 'J.K. Rowling',
        'price' => 25
    ]);
    $collection->pull('author'); // 'J.K. Rowling'
    $collection->all(); // ['title' => 'Harry Potter', 'price' => 25]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    集合 Shuffle 方法

    shuffle 方法将会随机排序集合中的项目。

    
    
    $collection = collect([1, 2, 3]);
    $shuffled = $collection->shuffle();
    $shuffled->all(); // [3, 1, 2] (random example)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    集合 Max 方法

    您可以使用 max 方法获取集合中的最大值。如果集合中包含数组,则可以传递参数以获取某个键的最大值。

    
    
    $max = collect([
        ['title' => 'Gift card', 'price' => 50],
        ['title' => 'Chair', 'price' => 80]
    ])->max('price'); // 80
    $max = collect([1, 2, 3])->max(); // 3
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    Attention Is All You Need:Transformer论文阅读笔记
    Java调用Web Service接口
    c++新闻发布系统(支持登录注册)
    Go之流程控制大全: 细节、示例与最佳实践
    软件测试定位bug方法+定位案例(详解)
    17-ETL工具、大数据架构、Flume介绍、Flume组件介绍
    初步认识 C语言
    回归(Regression)
    在数据分析中,对缺失值解决方案的分析
    Jackson ObjectMapper activateDefaultTyping 中 JsonTypeInfo 的作用
  • 原文地址:https://blog.csdn.net/weiguang102/article/details/126604437