skip to Main Content

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


  1. When you want to get all the data from a model, you have to use get() function:

    Posts::get();
    
    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
  3. It looks like by running Foo::all() or Foo::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 or get functions

    An all() call will return a collection where you can preform filtering after you collected the data
    and get() will require you to specify filtering before execution as its done on SQL level.

    Example:

    $exampleOne = Foo::where('bar', 1)->get(); //query builder
    $exampleTwo = Foo::all()->where('bar', 1); //Laravel collection
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search