here is my code
return DB::table('jobs')->Join('cities','jobs.city_id','=','cities.city_id')
->select('jobs.city_id', DB::raw('COUNT(jobs.city_id) AS num_jobs'))
->groupBy('jobs.city_id')
->orderBy('cities.city')
->limit($limit)
->get();
I want to get result order by alphabetically cities name which is in cities table row name as city and city_id in both tables jobs and cities, I also want number of jobs to show number of jobs in a specific city.
Thanks,
Change strict value to false in database.php file. It will works.
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => false, //Change this
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
Related
I am working on a cms project. I was created a portal and thenn new database connection created. So,there is a table users i wan that if someone create new user in that portal then it will be added to the master database that is another database. So, in laravel how can we make connection with another database and save the data of the portal to our master database table also.
Trying to make a sync connection that if in cms portal if admin create a user then that data must be submitted to the master table.
You can do it like this:
In .env add the following: note that you can customize your connection to another database, like postgres/mariadb etc.. check the documentation here for supported databases: https://laravel.com/docs/9.x/database#introduction
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=db
DB_USERNAME=user
DB_PASSWORD=password
DB_CONNECTION_SECOND=mysql
DB_HOST_SECOND=localhost
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=db2
DB_USERNAME_SECOND=user2
DB_PASSWORD_SECOND=password2
In config/database.php: note that you can customize your connection to another database, like postgres/mariadb etc.. check the documentation here for supported databases: https://laravel.com/docs/9.x/database#introduction
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'mysql2' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST_SECOND', '127.0.0.1'),
'port' => env('DB_PORT_SECOND', '3306'),
'database' => env('DB_DATABASE_SECOND', 'forge'),
'username' => env('DB_USERNAME_SECOND', 'forge'),
'password' => env('DB_PASSWORD_SECOND', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
In the sample model:
class Job extends Model
{
protected $connection = 'mysql2';
}
I want to sync my local database to online database when i click on a button in blade page. I looked online for the solution but did not found anything that worked for me.
I have updated my database .php file I have added local and server database connection credentials.
I have found a solution but that is not completed.
solution that i found
database.php
'local' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('LOCAL_DB_HOST', '127.0.0.1'),
'port' => env('LOCAL_DB_PORT', '3306'),
'database' => env('LOCAL_DB_DATABASE', 'forge'),
'username' => env('LOCAL_DB_USERNAME', 'forge'),
'password' => env('LOCAL_DB_PASSWORD', ''),
'unix_socket' => env('LOCAL_DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'server' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('SERVER_DB_HOST', '127.0.0.1'),
'port' => env('SERVER_DB_PORT', '3306'),
'database' => env('SERVER_DB_DATABASE', 'forge'),
'username' => env('SERVER_DB_USERNAME', 'forge'),
'password' => env('SERVER_DB_PASSWORD', ''),
'unix_socket' => env('SERVER_DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
controller code
// Connect to local database
return $local_database = DB::connection('local-db')->table('table_name');
// Get table data from production
foreach ($local_database->table('table_name')->get() as $data) {
// Save data to staging database - default db connection
DB::table('table_name')->insert((array) $data);
// This query can do only one time for multiple time we have to do
DB::table('table_name')->insertOrUpdate((array) $data);
// This query also update column only, if you do some changes in
//previous data it will not sync for that i dont know this query is
//correct or not but it worked.
DB::table('table_name')->delete((array) $data);
DB::table('table_name')->insertOrIgnore((array) $data);
}
env
SERVER_DB_CONNECTION=server
SERVER_DB_HOST=127.0.0.1
SERVER_DB_PORT=3306
SERVER_DB_DATABASE=sync_db
SERVER_DB_USERNAME=root
SERVER_DB_PASSWORD=
LOCAL_DB_CONNECTION=local
LOCAL_DB_HOST=127.0.0.1
LOCAL_DB_PORT=3306
LOCAL_DB_DATABASE=sync_db
LOCAL_DB_USERNAME=root
LOCAL_DB_PASSWORD=
I am developing a laravel application. I connect my laravel application with a mysql database. But I need a table data that is in another server and that database is sql database is in IIS server.
'default' => env('DB_CONNECTION', 'mysql'),
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => false,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=line
DB_USERNAME=root
DB_PASSWORD=
I need help to use two databases from different server (one from mysql and another from sql) on my same laravel application.
In your config/database.php
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => false,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
// Add this (Connect sql server)
//TCP port 1433 is typically the port used by a default instance of SQL Server
'sqlsrv' => [
'driver' => 'sqlsrv',
'url' => env(''),
'host' => env('DB_HOST', 'X.X.X.X'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'database_name'),
'username' => env('DB_USERNAME', 'user'),
'password' => env('DB_PASSWORD', 'password'),
'charset' => 'utf8mb4',
'prefix' => '',
'prefix_indexes' => true,
],
Get your mysql table
$mysql = DB::connection('mysql')->table('xxx')->select('*')->get();
Get your sql server table
$sqlsrv= DB::connection('sqlsrv')->table('xxx')->select('*')->get();
I need to append dynamically connection in the database.php file using PHP code, is it possible or not?
It is working for me
When I create a new customer then
public function setConnection($tenantName){
//GET Database Connection file path
$path = config_path('database.php');
//GET Database Connection file
$arr = include $path;
// load the array from the file
$new_connection=[
'driver' => 'mysql',
'host' => env('DB_HOST'),
'database' => $tenantName,
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false
];
// modify the array
$arr['connections'][$tenantName]=$new_connection;
// write it back to the file
file_put_contents($path, "<?php return " . var_export($arr, true) . ";");
}
It's possible to have multiple database connections and explicitly execute queries on one of them: https://laravel.com/docs/8.x/database#using-multiple-database-connections
The config/database.php has to return an array where config('database.connections') contains an array with keys representing different database engines but it's totally possible to for example add mysql2 and mysql3 to that array.
For example:
<?php
use Illuminate\Support\Str;
$customConnections = [];
for(var $i = 1; $i < 5; $i++) {
$customConnections['mysql'.$i] = [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '330'.$i),
'database' => env('DB_DATABASE', 'mysql'.$i),
'username' => env('DB_USERNAME', 'mysql'.$i),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
];
}
return [
/*
|--------------------------------------------------------------------------
| Default Database Connection Name
|--------------------------------------------------------------------------
|
| Here you may specify which of the database connections below you wish
| to use as your default connection for all database work. Of course
| you may use many connections at once using the Database library.
|
*/
'default' => env('DB_CONNECTION', 'mysql'),
/*
|--------------------------------------------------------------------------
| Database Connections
|--------------------------------------------------------------------------
|
| Here are each of the database connections setup for your application.
| Of course, examples of configuring each database platform that is
| supported by Laravel is shown below to make development simple.
|
|
| All database work in Laravel is done through the PHP PDO facilities
| so make sure you have the driver for your particular database of
| choice installed on your machine before you begin development.
|
*/
'connections' => [
...$customConnections,
'sqlite' => [
'driver' => 'sqlite',
'url' => env('DATABASE_URL'),
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE', 'homestead'),
'username' => env('DB_USERNAME', 'homestead'),
'password' => env('DB_PASSWORD', 'secret'),
'options' => [
// here you can pass more settings to the Mongo Driver Manager
// see https://www.php.net/manual/en/mongodb-driver-manager.construct.php under "Uri Options" for a list of complete parameters that you can use
'database' => env('DB_AUTHENTICATION_DATABASE', 'admin'), // required with Mongo 3+
],
],
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'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',
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
],
// ...
];
Laravel 5.8. I am trying to save an Eloquent model, choosing the database connection at runtime:
$catModel = new Cat;
if (env('USE_STAGING')) {
$catModel->setConnection('staging');
}
$cat = $catModel::create(['name' => $request->name]);
My config/database.php file:
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'staging' => [
'driver' => 'mysql',
'host' => env('STAGING_DB_HOST', '127.0.0.1'),
'port' => env('STAGING_DB_PORT', '3306'),
'database' => env('STAGING_DB_DATABASE', 'forge'),
'username' => env('STAGING_DB_USERNAME', 'forge'),
'password' => env('STAGING_DB_PASSWORD', ''),
'unix_socket' => env('STAGING_DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
],
My problem is that each time I run this code, the cat is created in the mysql (default) database, rather than the staging database, even when USE_STAGING is true.
The problem is the static call of the create() method.
By changing $catModel::create() to $catModel->create() it should use the correct connection:
$catModel = new Cat;
if (env('USE_STAGING')) {
$catModel->setConnection('staging');
}
$cat = $catModel->create(['name' => $request->name]);