Search code examples
laraveleloquentlaravel-query-builder

Laravel query builder or Eloquent both return null result


I am trying to return a single id from a query result. When I dump the query using the \DB::getQueryLog(), it shows the correct query. And when I plug the query into my DB manager, it returns the exact result I want.

I have tried the following two different approaches and both return null each time.

$drawings = DrawingsModel::query()
    ->select('id')
    ->from('drawings')
    ->where('draw_date', '=', $this->pc->options['start_date'])
    ->get()
    ->toArray();

dump(DB::getQueryLog());
dd($drawings);

Also tried this;

$drawings = \DB::query()
     ->select('id')
     ->from('drawings')
     ->where('game_id', '=', $game->id)
     ->where('draw_date', '=', $this->pc->options['start_date'])
     ->first();

dump(DB::getQueryLog());
dd($drawings);

This is the result of the getQueryLog();

1 => array:3 [
  "query" => "select `id` from `drawings` where `game_id` = ? and 
  `draw_date` = ? limit 1"
  "bindings" => array:2 [
     0 => 1
     1 => "2023-08-15"
  ]
  "time" => 3.91
]

Regardless of what method I try though, I always get null as my result. I feel like I have to be overlooking something. Any help would be greatly appreciated.


Solution

  • After days of digging, it turns out that putting where conditions in an array worked for solving my issue. The fix was as simple as replacing this;

    ->where('game_id', '=', $game->id)
    ->where('draw_date', '=', $this->pc->options['start_date'])->get();
    

    with this;

    ->where(['game_id', '=', $game_id],['draw_date', '=', $start_date])->get();