Deprecated: Hook custom_css_loaded is deprecated since version jetpack-13.5! Use WordPress Custom CSS instead. Jetpack no longer supports Custom CSS. Read the WordPress.org documentation to learn how to apply custom styles to your site: https://wordpress.org/documentation/article/styles-overview/#applying-custom-css in /home/everyuseful/public_html/wp-includes/functions.php on line 6085
DB Archives - EveryUseful

How to connect Laravel to SQL Server locally

If you want to set Laravel DB to be SQL Server, you need to edit .env file and add sqlsrv as your DB connection

DB_CONNECTION=sqlsrv
DB_HOST=127.0.0.1  
DB_PORT=1433
DB_DATABASE=YOUR_DB_NAME
DB_USERNAMEYOUR_USERNAME
DB_PASSWORD=YOUR_PASSWORD

You need to save the .env file and clear the config cache

php artisan config:clear

Also you need to download Microsoft Driver for PHP for SQLServer from this url:

https://learn.microsoft.com/en-us/sql/connect/php/download-drivers-php-sql-server?view=sql-server-ver16

Extract the downloaded file and copy the files that match your PHP version into the ext folder of your php folder

After that, you need to edit the php.ini (in your PHP folder) to add the drivers you just downloaded

extension=php_sqlsrv_82_ts_x64.dll
extension=php_pdo_sqlsrv_82_ts_x64.dll

Note that you have to edit the names of the extensions to match the driver files you just downloaded which match with PHP version, in my case it was 8.2 version

After saving the php.ini you must restart the apache server for changes to take effect

Now if you want to test the DB connection , you can open a terminal and go to tinker

php artisan tinker

and then type

DB::connection()->getDatabaseName()   

If you get the name of the DB, then the connection is good, but wait .. that doesn’t mean you are able to read or write from the DB. For example, if you tried to do a migrate and you get an error like this:

SQLSTATE[08001]: [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: The wait operation timed out.

 (Connection: sqlsrv, SQL: select * from sys.sysobjects where id = object_id(migrations) and xtype in (‘U’, ‘V’))

That means there is most probably a problem with the credentials or the port used

First, you need to make sure that the user has SQL Server authentication, not Windows authentication, you need to open SQL Server Management Studio and right on the Server from the left menu, click on Properties and then Security and choose SQL Server and Windows Authentication mode option

After clicking save, go to the left menu:  Security > Login and right click on the user name and click on Properties and make sure that the option of SQL Server Authentication is active

Better to restart the SQL Server by right click on the Server name from the left menu and click on Restart.

If you still get an error message, try to stop the SQL server Process from Task manager

Nothing to migrate in Laravel

If you are trying to migrate a single class you will do something like this

php artisan migrate --path = path_to_migration_file

if you get something like :

Nothing to migrate

and you are sure the file is there, then you can try to write the command like this

php artisan migrate --path="database/migrations/2022_11_04_1235_create_table.php"

How to view raw SQL of Laravel query?

There are a number of ways to view the raw SQL statement of your Larael query builder

->dd()

A simple way to show you the SQL statement along with its binding is replacing ->get(), first(), .. with ->dd(), here is an example:

$users = User::select('name')
                  ->where('id', '<', 20)
                  ->where('is_active', 1)
                  ->dd();

and the output will be like this:

"select `name` from `users` where `id` < ? and `is_active` = ?"
array:2 [▼
  0 => 20
  1 => 1
]
Continue reading “How to view raw SQL of Laravel query?”

Can’t access DB or phpmyadmin locally

If you got an error like: “Host ‘localhost’ is not allowed to connect to this MariaDB server” you can edit the file xampp\mysql\bin\my.ini

Add this line:
skip-grant-tables
after [mysqld] group.

Example:
[mysqld]
skip-grant-tables
port=3306
socket=/tmp/mysql.sock

After that, login to phpmyadmin and repair the users table as it mostly is corrupted.

Also try to rename :

xampp\mysql\data\ibdata1
to
xampp\mysql\data\ibdata1.bak