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

More than 3 years have passed since last update.

【Laravel8.16】ORMで指定したカラムだけ取得する方法

Posted at

Laravel歴2日の初心者です。
間違えてたらごめんなさい。

指定したカラムだけ取って来たい!!どないすんねん!!ってなったときの備忘録です。
公式マニュアルにも書いておらず、ググっても答えが出てこなかったのでソース直読みすることに。

クエリビルダとORMでの全件取得方法

クエリビルダ
$users = DB::table('users')->get();
ORM
$users= App\Models\Users::all();

指定したカラムだけ取得する方法

クエリビルダ
$users = DB::table('users')->select('name', 'email as user_email')->get();
ORM
$users= App\Models\Users::all('name', 'email as user_email');

ソース

Model.php
    public static function all($columns = ['*'])
    {
        return static::query()->get(
            is_array($columns) ? $columns : func_get_args()
        );
    }
1
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
1
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?