LoginSignup
2
1

More than 3 years have passed since last update.

Laravelの配列操作ラッパー、Collectionを単品で使用してみる

Last updated at Posted at 2020-09-11

概要

Laravelのcollectionが便利なので単品で使いたい
コレクション -Laravelドキュメント

install

% composer require illuminate/support

code

sample.php

<?php
require_once "vendor/autoload.php";

$array = ['key' => 'value'];
$collection = collect($array);
var_dump($collection);

結果

% php sample.php 
object(Illuminate\Support\Collection)#3 (1) {
  ["items":protected]=>
  array(1) {
    ["key"]=>
    string(5) "value"
  }
}

できた!

参考

おまけ

docker-compose.yml

version: '3'
services:
  php:
    container_name: php-collection
    build:
      context: .
      dockerfile: docker/php/Dockerfile
    volumes:
      - ./php:/var/www
    tty: true
    working_dir: /var/www
    env_file:
      - ./.env

Dockerfile

FROM php:7.4

COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

RUN cp /usr/share/zoneinfo/Asia/Tokyo /etc/localtime \
    && apt-get update \
    && apt-get install -y zip unzip

build & 起動 & 実行

docker-compose build php
docker-compose up -d
docker exec -it php-collection /bin/bash

composer require illuminate/support
vi ./sample.php
php ./sample.php
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1