I’m trying to fetch all records from a database table using Post::all()
. Strangely, it’s giving me a 500 error.
if I use a where clause like Post::where('id', 1)->get()
, it works just fine. I checked the error log, but there’s nothing there
I’m trying to fetch all records from a database table using Post::all()
. Strangely, it’s giving me a 500 error.
if I use a where clause like Post::where('id', 1)->get()
, it works just fine. I checked the error log, but there’s nothing there
3
Answers
When you want to get all the data from a model, you have to use get() function:
if you want to get all the data, and get error on this code
Posts::all()
you can use get function:
Posts::get();
DB::table('posts')->get();
you can write command in terminal
php artisan optimize:clear
It looks like by running
Foo::all()
orFoo::get()
you might be running into memory limit problems by sounds of it.It is not advice to fetch all records from the table unless the table is controlled and contains only few records
You can always redefine the query after/before you have ran
all
orget
functionsAn
all()
call will return a collection where you can preform filtering after you collected the dataand
get()
will require you to specify filtering before execution as its done on SQL level.Example: