5
2

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.

【Laravel】オレ得 Eloquent/QueryBuilderメモ集

Last updated at Posted at 2021-01-22

概要

よく忘れるので自分用メモ✏️
※日々追加...

データ取得

get

$users->get();
// 返り値は collection<StdClass>

first

$users->first();
// 返り値は StdClass

WHERE句

whereIn(含む)

$users->whereIn('id', [1, 2, 3]);

whereDate(日付)

$query->whereDate('created_at', '=', '2020-01-22');

JOIN句

LeftJoin

DB::table('users as U')
  ->select('U.name, U.age, J.role')
  ->leftJoin('jobs as J', 'j.user_id', '=', 'U.id');

AND/OR

AND検索

$query
  ->where('name', '=', $value1)
  ->where('age', '=', $value2);

OR検索

$query
  ->where('name', '=', $value1)
  ->orWhere('age', '=', $value2);

OR+AND検索

$query->where(function ($query) use ($keyword) {
     $query->where('name', 'LIKE', '%' . $keyword . '%')
           ->orWhere('age', 'LIKE', '%' . $keyword . '%');
});
5
2
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
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?