雑多メモ

2022/05/31

Bootstrap

ベース

補足情報

React Bootstrap

検索

Eloquent

IN句

->whereIn('KEY_NAME', [ARRAY, ...])

Illuminate\Support\Collection に対する処理

$employeesCollectionObject = $dbConnect->table($this->table)
                    ->where(
                        [
                            ['id', '=', $id],
                        ]
                    )
                    ->select(
                        'employee_id',
                        'depart',
                    )
                    ->get();
$employeesCollectionArray = $employeesCollectionObject->map(function ($item, $key) {
    return [
        'tori_id' => $item->employee_id,
        'domain' => $item->depart,
    ];
});

例えばこうすることで、

object(Illuminate\Support\Collection)[xxx]
  protected 'items' => 
    array (size=10)
      0 => 
        object(stdClass)[yyy]
          public 'employee_id' => string '001' (length=3)
          public 'depart' => string 'sales' (length=5)
      1 => 
        object(stdClass)[zzz]
          public 'employee_id' => string '002' (length=3)
          public 'depart' => string 'accounting' (length=10)
      2 => 
      ...

このようなコレクション→オブジェクトの配列が

object(Illuminate\Support\Collection)[xxx]
  protected 'items' => 
    array (size=10)
      0 => 
        array (size=2)
          'employee_id' => string '001' (length=3)
          'depart' => string 'sales' (length=5)
      1 => 
        array (size=2)
          'employee_id' => string '002' (length=3)
          'depart' => string 'accounting' (length=10)
      ...

このようなコレクション→連想配列の配列に。

Illuminate\Support\Collection を配列に変換

->toArray() or ->all()

さらに

$employeesCollectionArray = $employeesCollectionObject->map(function ($item, $key) {
    return [
        'tori_id' => $item->employee_id,
        'domain' => $item->depart,
    ];
});
$customersArray = $employeesCollectionArray->toArray();

こうすることで

array (size=10)
  0 => 
    array (size=2)
      'employee_id' => string '001' (length=3)
      'depart' => string 'sales' (length=5)
  1 => 
    array (size=2)
      'employee_id' => string '002' (length=3)
      'depart' => string 'accounting' (length=10)
  ...

配列→連想配列にできる。

DB:raw('COUNT(*) as hoge') を使用したい

DB はファサードだった。

ただし、

のように

use Illuminate\Support\Facades\DB;

を追加すると

A facade root has not been set.

で怒られてしまう。

のように Laravel 内で使用する場合はインスタンスとの紐付けが上手く行われるが、今回は Eloquent のみの単体使用なのでこのパターンではできない。

use \Illuminate\Database\Capsule\Manager as DB;

を Eloquent のクエリ使用箇所で再度読み込ませることでひとまず乗り越えた。ただし、

COUNT フィールドが正しくないか、構文エラーです。

と、引き続き COUNT で引っかかってしまうので要調査。

DB:raw()

サブクエリを使った JOIN

SQL

GROUP BY

PHP

連想配列からあるキーの値の配列のみを抽出

array_column([ARRAY, ...], 'KEY_NAME')

ヒアドキュメント


Written by Circle
A mound built by the accumulation of hyperlinks are like Kowloon.