0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Larevelのコレクション・メソッドについて ※データ取得編

Posted at

get()で指定したキーのデータを取得する

get()は一番基本的なメソッドで、PHPの配列のようにキーを指定してデータを取り出す。

$collection = ccollect([
    'id'=> 1,
    'name'=> '山田太郎', 
    'age'=> 30,
]);

echo $$collection->get('name'); // 山田太郎

キーがない場合は、インデックス番号で指定することでデータを取得できる。

$collection = ccollect([
    'id'=> 1,
    'name'=> '山田太郎', 
    'age'=> 30,
]);

echo $collection->get(1); // 1

オプションで、2番目の引数としてデフォルト値を渡すことも可能。


$collection = ccollect([
    'id'=> 1,
    'name'=> '山田太郎', 
    'age'=> 30,
]);

echo $collection->get('from','Tokyo'); //Tokyo

メソッドのデフォルト値としてコールバックを渡すことも可能。
指定されたキーが存在しない場合は、コールバックの結果が返される。

$collection->get('email',function(){
    return 'taro@gmail.com';
});

// taro@gmail.com

pluck()で指定したキーの配列を取得する

$collection =collect([
    ['product_id' => 1, 'product_name' => 'Product A', 'price' => 100],
    ['product_id' => 2, 'product_name' => 'Product B', 'price' => 200],
    ['product_id' => 3, 'product_name' => 'Product C', 'price' => 300],
]);

$products =$collection->pluck('product_name');

$plucked->all(); 

// ['Product A', 'Product B', 'Product C']

結果のコレクションにキーを指定して付ける方法もある。


$collection =collect([
    ['product_id' => 1, 'product_name' => 'Product A', 'price' => 100],
    ['product_id' => 2, 'product_name' => 'Product B', 'price' => 200],
    ['product_id' => 3, 'product_name' => 'Product C', 'price' => 300],
]);

$plucked=$collection->pluck('product_id','product_name');

$plucked->all();

//['Product A' => 1,'Product B' => 2,'Product C' => 3]

ネストされた値の取得も可能。


    [
    'id' => 1,
    'name' => '山田太郎',
    'speakers'=>[
        'speaker1'=>['a','b','c'],
        'speaker2'=>['d','e','f'],
    ],
],
    [
    'id' => 2,
    'name' => '鈴木一郎',
    'speakers'=>[
        'speaker1'=>['g','h','i'],
        'speaker2'=>['j','k','l'],
    ],
    ],
]};


$plucked = $collection->$plucked('speakers.speaker1');

$$plucked->all();

//['a','b','c','g','h','i']

toArray()で中身を配列で取得する

$collection = collect(['name'=>'Desk','price'=>100]);

$array = $collection->toArray();

//['name'=>'Desk','price'=>100]

all()で中身を配列で取得する

$collection = collect(['name' => 'Desk', 'price' => 100]);

$array = $collection->all();

//['name' => 'Desk', 'price' => 100]

take()で指定した件数だけデータ取得する

$collection = collect([0,1,2,3,4,5]);

$collect = $collection->take(3);

$collect =>all(); // [0, 1, 2]

負の整数を渡して、コレクションの最後から指定された数のアイテムを取得することもできる。

$collection = collect([0,1,2,3,4,5]);

$collect = $collection->take(-1);

$collect =>all(); // [5]

takeUntil()で特定の条件が来るまでデータ取得する

$collection = collect([0,1,2,3,4]);

$subset =$collection->takeUntil(function (int, $item) {
    return $item > 2;
});

$$subset->all(); // [0, 1, 2]

takeWhile()で特定の条件が続く限りデータ取得する


$collection = collect([0,1,2,3,4]);

$subset =$collection->takeWhile (function (int, $item) {
    return $item > 2;
});

$subset->all(); // [0, 1, 2]

toJson()でJSONデータを取得する

$collection =collect(['name' => 'John', 'age' => 30, 'city' => 'New York']);
$collect->toJson();
// {"name":"John","age":30,"city":"New York"}

diff()で指定データ「以外」のデータだけ取得する

$collection =collect(]1,2,3,4,5,6)];

$diff =$$collection->diff([2,4,6]);

//[1,3,5]

diffAssoc()でキーの中身が違っているものだけ取得する

$collection = collect([
    'color' => 'orange',
    'type' => 'fruit',
    'remain' => 6,
]);

$diff = $collection->diffAssoc([
    'color' => 'yellow',
    'type' => 'fruit',
    'remain' => 3,
    'used' => 6,
]);
$diff->all();

// ['color' => 'orange', 'remain' => 6]

diffKeys()でキーが存在しているデータだけ取得する

$collection = collect([
    'one' => 10,
    'two' => 20,
    'three' => 30,
    'four' => 40,
    'five' => 50,
]);

$diff = $collection->diffKeys([
    'two' => 2,
    'four' => 4,
    'six' => 6,
    'eight' => 8,
]);

$diff->all();

// ['one' => 10, 'three' => 30, 'five' => 50]

each()で1行ずつデータを取得する

$collection = collect(['鈴木', '田中', '佐藤']);
$colection->each(function (int $item int $$key) {
    echo $$key .':'. $item;
});

// 0:鈴木 
// 1:田中
// 2:佐藤
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?