Unsupported driver [mongodb] Laravel - php

I am using Laravel 5.1 with MongoDB. I already installed
https://github.com/jenssegers/laravel-mongodb
Problem is I am getting this error when running my queries:
Unsupported driver [mongodb]
But I already have my drivers installed in my machine. I am using Windows 10 and WAMP. I can confirm it with php info:
I
also check all my php.ini files which i already included my DLL's and looks fine. I have my drivers also.
My jessengers version is v3.0.2

I think you are missing the line :
Add the service provider in config/app.php:
Jenssegers\Mongodb\MongodbServiceProvider::class,
Refer the Documentation.
https://github.com/jenssegers/laravel-mongodb

Run this :
composer require jenssegers/mongodb:*

There are 2 mongodb drivers in php:
The legacy one: http://php.net/manual/en/book.mongo.php
The recommended one: http://php.net/manual/en/set.mongodb.php
laravel-mongodb Readme reads:
WARNING: The old mongo PHP driver is not supported anymore in versions >= 3.0
Laravel 5.1 can be used with either jenssegers/mongodb v2.2 or jenssegers/mongodb v3.0. The former requires legacy driver, the later requires new driver.
Check which version of jenssegers/mongodb you are using, and make it agree with php mongodb driver.

composer require jenssegers/mongodb:*
install jenssegers package by above command
then
composer update
you need to make database related changes in .env file :
DB_CONNECTION=mongodb
DB_HOST=localhost
DB_PORT=27017
DB_DATABASE=mongodb
DB_USERNAME=
DB_PASSWORD=
and in config/database.php file :
'default' => env('DB_CONNECTION', 'mongodb')
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE','mongodb'),
'username' => env(''),
'password' => env(''),
],
modify above parameter as per your appropriate credentials.
Also in config/app.php
Jenssegers\Mongodb\MongodbServiceProvider::class,

if you are using lumen, make sure you are adding the service provider before $app->withEloquent();
Like this
$app->register(Jenssegers\Mongodb\MongodbServiceProvider::class);
$app->withEloquent();

Related

CodeIgniter 3.1.9: Call to undefined function odbc_connect() in MacOS

UPDATE: This question has been flagged as a question that has been solved already. But when checked, those previous "similar" questions require users to download the corresponding .dll file which is not applicable for macOS users and other answers did not explicitly elaborate on how to install needed extensions like pdo_sqlsrv, sqlsrv, and ODBC for XAMPP-installed PHP.
I'm trying to connect to MS SQL database using CodeIgniter 3.1.9 via ODBC:
$db['default'] = array(
'dsn' => '',
'hostname' => 'x.x.x.x',
'username' => 'sa',
'password' => 'xxx',
'database' => 'xxx',
'dbdriver' => 'odbc'
But I'm getting this error:
An uncaught Exception was encountered
Type: Error
Message: Call to undefined function odbc_connect()
Filename: /Applications/XAMPP/xamppfiles/htdocs/gpweb/system/database/drivers/odbc/odbc_driver.php
Line Number: 141
PHP is installed via XAMPP. I checked the php.ini file to get information about ODBC and the line below is disabled because of the semi-colon in the beginning:
;extension=php_pdo_odbc.dll
I know it is irrelevant because I'm running on MacOS and .dll files are for Windows (but I still tried enabling it and with more errors):
A PHP Error was encountered
Severity: Core Warning
Message: PHP Startup: Unable to load dynamic library '/Applications/XAMPP/xamppfiles/lib/php/extensions/no-debug-non-zts-20160303/php_pdo_odbc.dll' - dlopen(/Applications/XAMPP/xamppfiles/lib/php/extensions/no-debug-non-zts-20160303/php_pdo_odbc.dll, 0x0009): tried: '/Applications/XAMPP/xamppfiles/lib/php_pdo_odbc.dll' (no such file), '/Applications/XAMPP/xamppfiles/lib/php/extensions/no-debug-non-zts-20160303/php_pdo_odbc.dll' (no such file)
Filename: Unknown
Line Number: 0
Has anyone encountered this issue before and how did you solve it? How can you enable ODBC in MacOS with PHP installed using XAMPP?
I found the solution, missing in php.ini
extension=php_odbc.dll
Unfortunately, macOS users that use XAMPP to install PHP have no way to install extensions like sqlsrv, pdo_sqlsrv, and ODBC, unlike in Windows, where they can download the necessary .dll files and enable extensions in the php.ini.
I've been using CodeIgniter 3.x.x for years to create web applications and MySQL as the database. I wanted to utilize the said PHP library with my other projects requiring MSSQL as the database since CI can do so, provided that you have installed the necessary extensions.
I have scoured the internet and there is really no definite solution to install extensions in XAMPP-installed PHP running macOS.
I gave up and installed PHP using homebrew instead. In your terminal, enter:
$ brew install php
Then installed sqlsrv extension (for M1 ARM64 users)
sudo CXXFLAGS="-I/opt/homebrew/opt/unixodbc/include/" LDFLAGS="-L/opt/homebrew/lib/" pecl install sqlsrv
sudo CXXFLAGS="-I/opt/homebrew/opt/unixodbc/include/" LDFLAGS="-L/opt/homebrew/lib/" pecl install pdo_sqlsrv
Then the ODBC extension:
HOMEBREW_NO_ENV_FILTERING=1 ACCEPT_EULA=Y
brew install msodbcsql17 mssql-tools
Since I cannot use the XAMPP-installed PHP, I have to use the CodeIgniter 4 to use the natively installed PHP:
composer create-project codeigniter4/appstarter project-root
After that, in the /project/app/Config/Database.php of my CodeIgniter 4 project, setup the connection to the database:
public $default = [
'DSN' => '',
'hostname' => 'x.x.x.x', // IP ADDRESS OF THE SQL SERVER
'username' => 'xxx', // USERNAME
'password' => 'xxx', // PASSWORD
'database' => 'xxx', // NAME OF DATABASE
'DBDriver' => 'SQLSRV', // DATABASE DRIVER TO BE USED
'DBPrefix' => '',
'pConnect' => false,
'DBDebug' => (ENVIRONMENT !== 'production'),
'charset' => 'utf8',
'DBCollat' => 'utf8_general_ci',
'swapPre' => '',
'encrypt' => false,
'compress' => false,
'strictOn' => false,
'failover' => [],
'port' => 1433, // DEFAULT PORT FOR SQL SERVER
];
This is not the solution to the original problem (XAMPP-installed PHP), but an alternative solution to connect to MSSQL database using PHP.
REFERENCE:
https://learn.microsoft.com/en-us/sql/connect/php/installation-tutorial-linux-mac?view=sql-server-ver16
https://www.codeigniter.com/user_guide/installation/index.html
But still, if you found a way to install sqlsrv and ODBC extensions in XAMPP-installed PHP running macOS, please don't hesitate to post it here.

Class 'Redis' not found in Lumen

Lumen Version: 6.0
PHP Version: 7.2
Database Driver & Version: MySql 5.7, Redis
Code
use Illuminate\Support\Facades\Redis;
Redis::set($key, $data, 'EX', $expiry);
in app.php
$app->withFacades();
$app->withEloquent();
$app->register(Illuminate\Redis\RedisServiceProvider::class);
$app->configure('database');
Using the above code gives Class 'Redis' not found error. This error occurs only when the below packages are installed.
"illuminate/redis": "^6.5",
"illuminate/mail": "^6.5",
"laravel/lumen-framework": "^6.0",
With below packages which has lower versions it works without any error/issues.
"laravel/lumen-framework": "^5.8.*",
"illuminate/redis": "^5.8",
"illuminate/mail": "^5.8",
So why is it giving error when packages are upgraded.
If you are using Laravel 8, in the database.php file, replace the following line:
'client' => env('REDIS_CLIENT', 'phpredis')
to:
'client' => env('REDIS_CLIENT', 'predis')
then, add the predis dependency with composer:
composer require predis/predis
You can modify config/database.php.
because lumen6 redis default drive used phpredis.
add .env file like this.
REDIS_CLIENT=predis
Make sure you set up the PHP Redis extension and enable it.
Even after you do that, you will need to register an alias for Redis in your app.php file. It's clear that you referenced it with your use statement, but that is only visible in the class you are "using" it. The PHP Redis connector will need to reference it from somewhere globally, which is in the app.php file. Laravel comes with this already set-up, but unfortunately Lumen doesn't.
To be safe, wrap it with a check on class existence.
This is how I fixed the problem.
#You already have this:
$app->register(Illuminate\Redis\RedisServiceProvider::class);
#Add the following right below
if (!class_exists('Redis')) {
class_alias('Illuminate\Support\Facades\Redis', 'Redis');
}
My steps to fix that in Lumen 7:
install "illuminate/redis" package: composer require illuminate/redis:"^7.0"
install "php-redis" on my CentOS7: yum --enablerepo=epel -y install php-pecl-redis
install "predis" package: composer require predis/predis:"^1.0"
change the redis client to "predis" (by default its "phpredis"): 'client' => 'predis'. So the config is:
'redis' => [
'cluster' => false,
'client' => 'predis',
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DATABASE', 0),
],
]

PDOException with message 'could not find driver' Error for Laravel and Postgresql

First of all, the problem is about Laravel not postgresql or PHP. I can connect postgresql with a simple PHP file. But Laravel can't do it somehow.
When I try to connect postgresql server in my computer with laravel I get "PDOException with message 'could not find driver'" error. I am getting this error when I run DB::connection()->getPdo(); command at artisan tinker.
If I run php artisan migrate command, the error is Illuminate\Database\QueryException : could not find driver (SQL: select * from information_schema.tables where table_schema = public and table_name = migrations and table_type = 'BASE TABLE')
My configuration is below:
Windows 10
Wamp Server 3.1.4
Apache 2.4.35
PHP 7.2.10
Laravel Framework 6.0.3
Related lines of Laravel .env file:
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=laravel_dnm1
DB_USERNAME=postgres
DB_PASSWORD=mrd.BE.265
Related lines of Laravel database.php file:
'default' => env('DB_CONNECTION', 'pgsql'),
...
'pgsql' => [
'driver' => 'pgsql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'schema' => 'public',
'sslmode' => 'prefer',
],
When I run print_r(PDO::getAvailableDrivers()); at my server I get below output:
Array ( [0] => mysql [1] => pgsql [2] => sqlite )
Related lines of php info is below:
NOTE: There is no problem when I use mysql instead of postgresql.
NOTE2: I can connect the DB when I use regular PHP. Only Laravel gives this error.
install postgresql
sudo apt-get install php-pgsql
then uncomment pgsql and pdo-pgsql extensions in etc/php/$PHP_VERSION/apache2/php.ini file
then restart apache2
Verify your PHP version with php -v
Install php7.2-pgsql when needed.
I solved the issue. It is a bug at WAMP I think. When I edit php.ini from WAMP's menu it opens a different php.ini than active PHP version's. So I opened php.ini from file system and edited it from there.
There are different php version installed on your WAMP SERVER. when you click on WAMP icon it will show that pgsql and pdo_pgsql are active but for different php version. You need to check your php version using php -v command and then go to the WAMP folder and find that php version and edit php.ini file and enable pgsql extensions there.

I'm using Laravel 5 and I'm not being able to migrate my database using php artisan migrate

I'm using Laravel 5 but I'm not being able to migrate my database table. I have a macbook pro and I'm using Terminal. I'm using php artisan command:
php artisan migrate.
When I execute this command, I get the following error message: [PDOException]
SQLSTATE[HY000] [2002] Connection refused.
I have configured my database.php following the official tutorial videos on laracasts.com. My database.php file looks like the following:
...
'fetch' => PDO::FETCH_CLASS,
...
'default' => env('DB_CONNECTION', 'sqlite'),
...
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => database_path('database.sqlite'),
'prefix' => '',
],
....
I have read many comments on stackoverflow.com about this issue. Most of them are talking about modifying the ".env" file. The thing is I can't find this file! Which makes me wonder if my installation of Laravel is complete or not! I read that my ".env" file might be overriding my "database.php" file but I can't file the ".env" file!
The env file is most likely hidden.
Run defaults write com.apple.finder AppleShowAllFiles YES and Killall Finder in Terminal.
Open Finder and you should be able to see the .env file.
Edit your .env file.

Laravel migration and PostgreSQL schemas

I'm using PostgreSQL and want to try Laravel.
First - my up() function:
public function up()
{
Schema::create('entries.entries', function($t) {
$t->increments('id');
$t->string('username', 50);
$t->string('email', 100);
$t->text('comment');
$t->timestamps();
});
}
And i have two questions:
1) I haven't shema entries in my database, so, how can i change my up function to create it too? I dont want to do it manually.
2) I got an error when executed migration:
sudo php artisan migrate
[Illuminate\Database\QueryException]
SQLSTATE[3F000]: Invalid schema name: 7 ERROR: no schema has been selected
to create in (SQL: create table "emigrations" ("migration" varchar(255) not null,
"batch" integer not null))
How can i fix it?
Well i know that this question has more than 2 year's... but for the future people that reaches this. Laravel does not recognize the default 'public' schema of postgres by default and that's why you get the:
[Illuminate\Database\QueryException]
SQLSTATE[3F000]: Invalid schema name: 7 ERROR: no schema has been selected
to create in (SQL: create table "emigrations" ("migration" varchar(255) not null,
"batch" integer not null))
Thus to solve this error you just rename your Schema in postgres to anything besides public and, in your laravel project in the file config/database.php change the name of your pgsql conection schema to the one you want to use in your DB.
This should solve it.
It did for me anyways...
1) I don't think there is such functionality in Laravel but you can use an external package like https://github.com/pacuna/Laravel-PGSchema
2) Did you remove the default schema in your app/config/database.php file?
'pgsql' => array(
'driver' => 'pgsql',
'host' => 'localhost',
'database' => 'forge',
'username' => 'forge',
'password' => '',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
),
Laravel and PostgreSQL DB Connection, Authentication, Migration:
Prerequisites*
PHP 7.2+
Composer (a dependency manager for PHP)
PostgreSQL 9.5+
Installing Laravel
Create a new project with auth scaffolding.
laravel new blog --auth
Now Change default db connection in blog\config\database.php
'default' => env('DB_CONNECTION', 'mysql'),
change to
'default' => env('DB_CONNECTION', 'pgsql'),
Next, we’ll need to configure our database credentials in Laravel by updating the .env file:
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=projectmanager
DB_USERNAME=postgres
DB_PASSWORD=1234
Finally, we have to enable the PostgreSQL driver in our php.ini file. Let’s go into our XAMPP/WAMP directory and modify this file by commenting out these two drivers:
- extension=pdo_pgsql
- extension=pgsql
Note: check php.ini file location is bin\php\php7.3.5. Also, check the php.ini file is PHP or apache file.
Then Run migration for creating the table.
php artisan migrate
Now you get your final output.
Thanks and best of luck.

Categories