27
9

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】クエリビルダした結果のreturnを消してddしてた人

Posted at

なんのこっちゃ

たとえば

public function search($name)
{
  return DB::table('users')->where('name', $name)->get();
}

これの取得結果を確認したいときに dd() を使いたい。
――で、一旦 return を消してこんなふうに書いて検証していた。

public function search($name)
{
  $test = DB::table('users')->where('name', $name)->get();
  dd($test);
}

他にもやり方はあると思うが。

もっと簡単にできる

一番後ろに ->dd() を付けるだけでできる。知らなかった。

public function search($name)
{
  return DB::table('users')->where('name', $name)->get()->dd();
}

Laravel公式マニュアル『クエリビルダ』の『デバッグ』に書かれていた(一番下)。

Laravel 6.x データベース:クエリビルダ
https://readouble.com/laravel/6.x/ja/queries.html

マニュアルには書かれていなかったけどバージョン5.5でもできた(5.8のマニュアルから追記されるようになったぽい)。

27
9
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
27
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?