如何在Laravel中向集合添加新值?

PHPz
发布: 2023-09-11 12:02:01
转载
889 人浏览过

如何在Laravel中向集合添加新值?

Collection in Laravel是一个API包装器,它帮助您处理在数组上执行的不同操作。它使用IlluminateSupportCollection类来处理Laravel中的数组。

要从给定的数组创建一个集合,您需要使用collect()辅助方法,它返回一个集合实例。之后,您可以在集合实例上使用一系列方法,如转换为小写,对集合进行排序。

Example 1

的中文翻译为:

示例1

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;

class UserController extends Controller{
   public function index() {
      $mynames = collect(['Andria', 'Josh', 'James', 'Miya', 'Henry']);
      print_r($mynames);
   }
}
登录后复制

输出

当您在浏览器中测试相同内容时,您将获得以下输出−

Illuminate\Support\Collection Object(
   [items:protected] => Array(
      [0] => Andria
      [1] => Josh
      [2] => James
      [3] => Miya
      [4] => Henry
   )
   [escapeWhenCastingToString:protected] =>
)
登录后复制

要添加新值,您可以使用集合上的 push()put() 方法。

示例 2

使用push()方法。

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;

class UserController extends Controller{
   public function index() {
      $mynames = collect(['Andria', 'Josh', 'James', 'Miya', 'Henry']);
      $mynames->push('Heena');
      print_r($mynames);
   }
}
登录后复制

输出

上述代码的输出是 -

Illuminate\Support\Collection Object(
   [items:protected] => Array(
      [0] => Andria
      [1] => Josh
      [2] => James
      [3] => Miya
      [4] => Henry
      [5] => Heena
   )
   [escapeWhenCastingToString:protected] =>
)
登录后复制

示例 3

使用put()方法

当您有一个带有键:值对的集合时,使用put()方法

['firstname' => 'Siya', 'lastname' => 'Khan', 'address'=>'xyz']
登录后复制

让我们利用put()方法将一个键值对添加到上述集合中。

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;

class UserController extends Controller{
   public function index() {
      $stdDetails = collect(['firstname' => 'Siya', 'lastname' => 'Khan', 'address'=>'xyz']);
      $stdDetails->put('age','30');
      print_r($stdDetails);
   }
}
登录后复制

输出

上述代码的输出是 -

Illuminate\Support\Collection Object(
   [items:protected] => Array(
      [firstname] => Siya
      [lastname] => Khan
      [address] => xyz
      [age] => 30
   )
   [escapeWhenCastingToString:protected] =>
)
登录后复制

Example 4

的中文翻译为:

示例4

使用带有数组值的集合推送。

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;

class UserController extends Controller{
   public function index() {
      $myNames = collect([
         ['userid'=>1, 'name'=>'Andria'],
         ['userid'=>2, 'name'=>'Josh'],
         ['userid'=>3, 'name'=>'James']
      ]);
      $myNames->push(['userid'=>4, 'name'=>'Miya']);
      print_r($myNames);
   }
}
登录后复制

输出

上述代码的输出是 -

Illuminate\Support\Collection Object(
   [items:protected] => Array(
      [0] => Array(
         [userid] => 1
         [name] => Andria
      )

      [1] => Array(
         [userid] => 2
         [name] => Josh
      )

      [2] => Array(
         [userid] => 3
         [name] => James
      )

      [3] => Array(
         [userid] => 4
         [name] => Miya
      )
   )
   [escapeWhenCastingToString:protected] =>
)
登录后复制

以上是如何在Laravel中向集合添加新值?的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!