0
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.

Laravelで特定のレコードを除外して取得する

Posted at

特定のレコードを除外して取得する

詳細ページに移行して、その詳細ページを除いた他のページを候補として出したいなど、
それ以外の所得したい場合があると思います。
今回はその方法をアウトプットしていきます。

WhereNotInを利用する場合

第一引数でカラムを指定
第二引数で除外したいレコードのid配列を入れる
第三引数でstrictを厳格化

Test::whereNotIn('id', [$testId], true);

filter

filter()に関してはドキュメントを読んでみましょう
https://readouble.com/laravel/5.8/ja/collections.html#method-filter

filterメソッドは指定したコールバックでコレクションをフィルタリングします。テストでtrueを返したアイテムだけが残ります。

Test::filter(fn (Test $test) => $test->id !== $testId);

これで取得できてしまいます

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?