Laravel exists() & doesntExist() – Determine data existence

Tram Ho

Use exists() and doesntExist()

1. exists()

Instead of:

Please use:

2. doesntExist()

Instead of:

Please use:

3. Compare

With mysql laravel will compile to:

  • count()

    Counts how many rows of data meet the condition. Had to run from beginning to end of data table to compare and count => slow .
  • exists()

    If the data line is found, it will return true, otherwise it will return false. The best case scenario is that the data line that meets the condition is at the beginning of the data table.

4. Conclusion

  • Query speed is faster than using count() .
  • The code will be more semantically explicit (exists: exists – does not exists: does not exist).

5. Reference

6. About me

Hope this sharing will help you newbie a bit in the process of learning about Laravel.

I’m Cong Thanh Thank you for following my article, if you have any questions, please comment below.

Share the news now

Source : Viblo