douwang6635 2018-12-04 12:57
浏览 373
已采纳

在Laravel中制作数组大写的第一个字母

I'm trying to retrieve strings from my database using Laravel Eloquent queries. However, the data I get is in full caps. I want only the first letter to be in uppercase. Can anybody help me out?

$main = DB::table('master_accountsmain')->get()

I'm passing the main to my views using...

return view('home', 'main' => $main);

I have tried using ucfirst($main), but it does not work. The data remains in uppercase.

  • 写回答

3条回答 默认 最新

  • dongpu8935 2018-12-04 13:04
    关注

    ucfirst(strtolower($main[0]->property)) is what you want to do.

    $main is an array of objects because you're using ->get();

    If you were fetching just one with ->first(); it would return an eloquent object and you would simply call ucfirst(strtolower($main->property))

    If you have more than one result you could simply loop over or use fancy array functions builtin to php.

    If it is an eloquent collection they have built in methods such as ->each() that allow you to map an array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝