phplaraveleloquent

Why can't laravel/eloquent find certain table?


I'm trying to fetch some data from a table called "category", but laravel is throwing the

"SQLSTATE[42S02]: Base table or view not found" error.

I've tried with other table names like "company" and it works perfectly. Both of these tables exist but one of them can not be found with the same code.

This throws the error:

public static function getCategories()
    {
        $categories = [];

        $cat = DB::table('category')->get();
        if (isset($cat)){
            foreach ($cat as $category_name){
                array_push($categories, $category_name);
            }
            return json_encode($categories);
        }
        return null;
    }

This works as expected (same code except for the table name string):

public static function getCategories()
    {
        $categories = [];

        $cat = DB::table('company')->get(); //table name changed
        if (isset($cat)){
            foreach ($cat as $category_name){
                array_push($categories, $category_name);
            }
            return json_encode($categories);
        }
        return null;
    }

The only difference between those two tables is table collation:
company: utf8_general_ci
category: utf8mb4_swedish_ci


Solution

  • Ok, I found out we had two separate database-servers with almost identical data in them. Both had databases and tables with the same names, but one was missing the category table. Of course I was connected to the wrong server from laravel after all.

    TL;DR: Simple user error in configuration.