Thank you for your answer, unfortunately, it doesn't work. I tried with get, first, and paginate.
In real usage, I have a data-table for Foo and trying to sort the order by its Bar creation date, so I should go with either get or paginate.
I do have other Foo relationships and I'm also using the same subquery method as above for sorting, they are works just fine since they are using the same database connection.
Example other relation that works:
class Foo extends Model
{
$connection = 'sqlite';
$table = 'foos';
public function bar()
{
$this->belongsTo(Bar::class);
}
public function baz()
{
$this->belongsTo(Baz::class);
}
}
class Bar extends Model
{
$connection = 'mysql';
$table = 'bars';
public function foos()
{
$this->hasMany(Foo::class);
}
}
class Baz extends Model
{
$connection = 'sqlite';
$table = 'bazzes';
public function foos()
{
$this->hasMany(Foo::class);
}
}
Notice that Foo has the same database connection as Baz and sorting with code below works fine:
Foo::orderByDesc(
Baz::select('created_at')->whereColumn('id', 'foos.baz_id')->limit(1)
)->get()