LoginSignup
0
0

More than 5 years have passed since last update.

first()とget()、all()について

Posted at

今回は、Laravelのクエリービルダークラスのメソッドであるfirst()とget()、all()について整理します。

このfirst()とget()、all()は、クエリービルダーやEloquent ORMで使用します。
それぞれの使い方は、下記の通りです。

first()
クエリを実行して、レコードの最初の結果を取得します。
引数はオプションで、表示するカラム名を配列で指定します。
デフォルトは全カラムです。

記述例:
//クエリービルダー
DB::table('モデル名')->first();
//Eloquent ORM
モデル名::first();

get()
クエリを実行して、レコードの複数の結果を取得します。
条件または引数を指定しない場合、全レコードが取得されます。

記述例:
//クエリービルダー
DB::table('モデル名')->get();
//Eloquent ORM
モデル名::get();

all()
クエリを実行して、レコードの全ての結果を取得します。

記述例:
//クエリービルダー
DB::table('モデル名')->all();
//Eloquent ORM
モデル名::all();

【参考サイト】
『Laravel4、テーブル操作の戻り値』
『Queryビルダークラスのメソッド一覧』
『Builder::first()メソッド』
『Builder::get()メソッド』

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