Предполагая, что вы говорите о соединении PDO, вам нужно установить Resolver Connection для рассматриваемой модели, посмотрите на этот пример при использовании User::class
// setup
$databaseName = 'my_database_name';
$tablePrefix = '';
$pdo = new PDO("mysql:host=127.0.0.1;dbname=$databaseName", 'root', '');
$container = new \Illuminate\Container\Container(); // you should use the app container, this is just a placeholder
// 1st step - we need to create a custom factory to expose the base method responsible for doing the work under the hood
$customFactory = new class($container) extends \Illuminate\Database\Connectors\ConnectionFactory
{
/**
* @param PDO $pdo
* @param string $database
* @param string $prefix
* @return \Illuminate\Database\Connection
*/
public function makeConnection(PDO $pdo, $database, $prefix = '')
{
return $this->createConnection($pdo->getAttribute(PDO::ATTR_DRIVER_NAME), $pdo, $database, $prefix);
}
};
// 2nd - we use the method that we just created to convert the PDO instance into a Laravel connection
$connection = $customFactory->makeConnection($pdo, $databaseName, $tablePrefix);
// 3rd - we need to configure our connection resolver, you can use the above method to create you own instance, or you can use the one provided by the container
$resolver = new \Illuminate\Database\ConnectionResolver();
$resolver->addConnection('custom', $connection);
$resolver->setDefaultConnection('custom');
// 4th - assign the resolver to your model
User::setConnectionResolver($resolver);
// 5th - profit
dump(User::all());