laravel框架帮助函数

第一、数组的帮助函数:

1> head():返回数组中第一位:

Route::get('/my_home/helper', function(){
    $arr = [
       1,2,3
    ];
    return head($arr);                                            结果:返回1
});

2>array_only():在一个数组中取出想要的值,并重新组成数组;他有俩个参数(数组,[想要去的name]):

Route::get('/my_home/helper', function(){
    $arr = [
        'name'=>'lisi',
        'age' => 18,	
        'job' =>'worker',
    ];
    return array_only($arr,['name','age']);			  结果:{"name":"lisi","age":18}
});

3>array_first():过滤,俩个参数:第一个数组,第二个函数,指定过滤规则;

Route::get('/my_home/helper', function(){
    $arr = [10, 20, 30];
    return array_first($arr,function($key, $val){                        
        return $val >20;                                           结果:返回30
    });
});

4>array_add():添加

Route::get('/my_home/helper', function(){
    $arr = [
        'name'=>'lisi',
        'age' =>18
    ];
    return array_add($arr,'job','worker');			    结果:{"name":"lisi","ahe":18,"job":"worker"}
});

5>array_except():返回除去那个key

Route::get('/my_home/helper', function(){
    $arr = [
        'name'=>'lisi',
        'age' =>18,
        'job' =>'worker'
    ];
    return array_except($arr,'job');				     结果:{"name":"lisi","age":18}
});

6>array_flatten():返回最终值:

Route::get('/my_home/helper', function(){
    $arr = [
        'a'=>1,
        'b' =>[
            'a'=>2,
            'b'=>3
        ]
    ];
    return array_flatten($arr);					      结果:[1,2,3]
});

7>array_where():

Route::get('/my_home/helper', function(){
    $arr = [
        'name'=>'lisi',
        'age'=>18,
        'job'=>'worker'
    ];
    return array_where($arr,function($k, $v){
        return is_string($v);						结果:{"name":"lisi","job":"worker"}
    });
});

8>last():返回最后一个值:

Route::get('/my_home/helper', function(){
    $arr = [
       1,2,3
    ];
    return head($arr);                                                  结果:返回3
});

第二、路径(path)帮助函数

1>app_path():返回项目中的绝对路径

Route::get('/my_home/helper', function(){
   return app_path();                                                    结果:E:\web\htdocs\lar\app
});

2>config_path():返回的是config的路径

Route::get('/my_home/helper', function(){
   return config_path();						结果:E:\web\htdocs\lar\config
});

3>public_path():返回的是public路径

Route::get('/my_home/helper', function(){
   return public_path();						结果:E:\web\htdocs\lar\public
});

4>storage_path():返回storage路径

Route::get('/my_home/helper', function(){
   return storage_path();						结果:E:\web\htdocs\lar\storage
});

第三、字符串(string)帮助函数

1>str_plural()将单数变为复数,如:sheep它是单复数同行就不会改变的

Route::get('/my_home/helper', function(){
   return str_plural("apple");						结果:apples
});

2>starts_with():判断字符串是否已某一个开始的,返回布尔值

Route::get('/my_home/helper', function(){
   dd(starts_with("apple", 'a'));
});

3>starts_with():判断字符串是否已某一个结束的,返回布尔值

4>camel_case():转换成驼峰命名

"helloWorld"
Route::get('/my_home/helper', function(){ dd(camel_case("hello_world")); 结果:helloWorld});

5>class_basename():

Route::get('/my_home/helper', function(){
   dd(class_basename("App\Http\Controllers\Controller"));		结果:Controller
});

6>str_limil():限制字符串长度

Route::get('/my_home/helper', function(){
   dd(str_limit("abcd",3));						结果:abc...
});

7>str_is():判断某个字符串是否满足某一个规则,返回布尔值,第一个参数的条件

Route::get('/my_home/helper', function(){
    dd(str_is('ab*','abcd'));
});














  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值