3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Laravel】Eloquentで1つのカラムの情報を取得

Last updated at Posted at 2022-11-11

はじめに

Eloquentで1つのカラムの情報を取得する方法について調べた際にいくつかの方法が出てきたので、取得した結果をまとめてみました。

selectを使って取得

取得方法

$name_data = CategoryModel::select('name')->get();

結果

[{"name":"氏名"},{"name":"電話番号"},{"name":"性別"},{"name":"郵便番号"},{"name":"市区町村以下"},{"name":"都道府県"},{"name":"テスト"},{"name":"生年月日"}]  

getを使って取得

取得方法

$name_data = CategoryModel::where('chain_id', $request['chain_id'])
            ->get(['name'])

結果

[{"name":"氏名"},{"name":"電話番号"},{"name":"性別"},{"name":"郵便番号"},{"name":"市区町村以下"},{"name":"都道府県"},{"name":"テスト"},{"name":"生年月日"}]  

selectを使って取得した場合と同じですね。
書く量が減るのでこちらの方がいいかもしれないですね!

pluckを使って取得

取得方法

$name_data = CategoryModel::where('chain_id', $request['chain_id'])
            ->pluck('name');

結果

["氏名","電話番号","性別","郵便番号","市区町村以下","都道府県","テスト","生年月日"]

終わりに

以上、Eloquentで1つのカラムの情報を取得する方法とその出力結果について調べた結果でした。

3
1
1

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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?