When creating a query using the syntax DB::table(\'foo\')
, it creates a generic class (stdClass). Is there any way to cast the resulting rows to a specific class?>
Eloquent models dont have the hydrate() method.
This has been moved to the Eloquent Builder class.
https://laravel.com/api/5.4/search.html?search=hydrate
Here is my working example in some laravel 5.8 code:
$instance = new $class;
$table = $instance->getTable();
/** @var \Illuminate\Database\Eloquent\Builder $eloquent_builder*/
$eloquent_builder = new \Illuminate\Database\Eloquent\Builder(
// the Query Builder!
DB::connection($connection_name)
->table($table)
->select($columns)
->orderBy($order_by, $order)
);
// Tell Eloquent what you're using to hydrate the query
$eloquent_builder->setModel($instance);
return $eloquent_builder->get();