Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

denewey's avatar

Eloquent Query Not Working Where It works Otherwise

I have a couple simple Eloquent queries. The second one pulls data based on the first query. The first query pulls the correct data. The second pulls no data despite the same query working in my pgAdmin and in Tinker. The first query is in the following method:

function getCompanies($def=0)
{
    $user = Auth::user();
    $companies = Collection::make([$user->company]);

    if ($user->hasRole('sys_admin') || ($user->hasRole('comp_admin') && $def == 1)){
        $companies = Company::all();
    }

    return $companies->sortBy('name')->values();
}

This returns the result:

[{"id":1,"name":"First Company"},{"id":7,"name":"Second Company"}]

The second query, also in its own method:

function getControllers()
{
    $companies = getCompanies();
    \DB::enableQueryLog();
    $controllers = Controller::whereIn('company_id', $companies->pluck('id')->toArray())->get();
    \DB::getQueryLog();
    $controllers->sortBy('name')->values();
}

As you can see I used the QueryLog to return the exact query that Eloquent is creating. The result of this is:

"query":"select * from \"controllers\" where \"company_id\" in (?, ?)","bindings":[1,7],"time":2.65

When I run this query in my pgAdmin, or even in Tinker, I get the data I'm looking for, but in the code it gives no result. Why would the Eloquent query not run when the query that is created does run in the DB?

0 likes
5 replies
denewey's avatar

@sr57 - Thank you, I missed that - after a while the code starts looking like chopped cabbage. However, that didn't fix my issue. I'm still not getting data from the Eloquent query.

SilenceBringer's avatar
Level 55

@denewey what do you have if you dump the result of query?

function getControllers()
{
    $companies = getCompanies();
    $controllers = Controller::whereIn('company_id', $companies->pluck('id')->toArray())->get();
	dd($controllers);
	// ...

}

Note: if you use spatie/laravel-permission package - you can replace $user->hasRole('sys_admin') || ($user->hasRole('comp_admin') with $user->hasAnyRole('sys_admin', 'comp_admin') https://spatie.be/docs/laravel-permission/v5/basic-usage/role-permissions#content-checking-roles

denewey's avatar

@silencebringer - interesting! I get a collection of the data I'm looking for yet it doesn't show up when I log it and it doesn't show up when it gets passed (values have been changed to protect the innocent):

Illuminate\Database\Eloquent\Collection {#1829 ▼
  #items: array:1 [▼
    0 => App\Controller {#1825 ▼
      #dates: array:1 [▶]
      #visible: array:2 [▶]
      #appends: array:1 [▶]
      #client: null
      #connection: "pgsql"
      #table: "controllers"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #escapeWhenCastingToString: false
      #attributes: array:12 [▼
        "controller_id" => 1
        "controller_name" => "Pzx -9 PR"
        "api_version" => "3.0"
        "firmware" => "5.2.1.0.515"
        "ip" => "xxx.xxx.xxx.xx"
        "port" => 8443
        "login" => "charbor"
        "password" => "hauseser!23"
        "added_date" => "2022-01-14 03:45:54.10285+00"
        "deleted_date" => null
        "deleted" => false
        "company_id" => 7
      ]
      #original: array:12 [▶]
      #changes: []
      #casts: []
      #classCastCache: []
      #attributeCastCache: []
      #dateFormat: null
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #fillable: []
      #guarded: array:1 [▶]
    }
  ]
  #escapeWhenCastingToString: false
}

What causes that?

denewey's avatar

@silencebringer - This has solved my issue. I am able to move on. There's something I'm not understanding, though. I attempted to write the results of the $controllers query to a log file, but it only came up as an empty array. Why would that be?

Please or to participate in this conversation.