I came across an interesting problem today. I was Using laravel’s PHPUnit test framework to test a Eloquent class in my local environment. When I ran simple tests, I got that laravel eloquent find i was always returning null. Printing query log and running them was not helping.
So, I guessed whether laravel is actually connecting to my local database at all. So, I tried printing
In my test function. And surprisingly, PHPUnit outputted production servers database configurations! It should have used the environment I am testing! Anyway, that’s an architectural problem. So, here is the fix:
Copy the ‘database.php’ from your local environment to testing environment.
‘app/config/local/database.php’ to ‘app/config/testing/database.php’
It will work with this manual trick.
But remember now, if you want to test in production server, “which we never do”, PHPUnit will use local database connection!
Laravel Eloquent::Find() is returning null