0
1

More than 1 year has passed since last update.

Laravelで直接SQL文を実行する

Posted at

Laravelで直接SQLを実行するにはその種類によってメソッドが変わります。参照系の場合には DB::select() を利用します。

$sql = "select i.* from input_tmps i
where i.id is not null;";
$rows = DB::select($sql);

$retArray = [];
/** 結果取得 **/
foreach($rows as $row) {
  $retArray[$row->id][] = $row->value;
}

LOCK文等の場合には DB::unprepared() を使います。

DB::unprepared( 'LOCK TABLES ' .
  DB::getTablePrefix() . 'input_tmps1 WRITE,' .
  DB::getTablePrefix() . 'input_tmps2 WRITE,' .
  DB::getTablePrefix() . 'input_tmps3 WRITE, ' .
  DB::getTablePrefix() . 'input_tmps4 WRITE'
);
0
1
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
1