10
11

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 5 years have passed since last update.

【Laravel】Eloquentを使って単一項目&重複は削除して取得

Posted at
id pref city
1 北海道 札幌市
2 北海道 函館市
3 北海道 小樽市
4 青森県 五所川原市
5 青森県 平川市
6 岩手県 宮古市
7 岩手県 一関市
8 岩手県 奥州市

都道府県だけ欲しい時。

PrefCity::groupBy('pref')->get(['pref']);
  • groupBy で重複削除
  • get(['カラム名']) で単一項目取得

これで取得できる。

        {
            "pref": "北海道"
        },
        {
            "pref": "青森県"
        },
        {
            "pref": "岩手県"
        },
10
11
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
10
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?