I am implementing emoji support for my forum and I'm facing some issues.
Emoji is saved as? in database, but is shown correctly when i render it in my page.This page use twemoji , so the whole document body is parsed.
Here is my database config:
'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,
],
Here are columns that will contain emojis, so charset is correct.
Here is the view:
In my view , I am using twitter emoji , so emojis are shown correctly.My only concern is that,mysql shows them as ?
Check your MySQL tables, they are probably just set to UTF8. In order to support emojis, you should alter them to utf8mb4.
Related
I am struggling in connecting Laravel app with MYSQL database. I can connect to database over MySQL Workbench and also with PDO connection custom string but with Laravel I cannot. Not sure what is wrong.
Custom connection string: It works file is inside laravel app. I put this just for checking
$conn = mysqli_init();
mysqli_ssl_set($conn,NULL,NULL, "BaltimoreCyberTrustRoot.crt.pem", NULL, NULL) ;
mysqli_real_connect($conn, 'sever.host', 'server-username', 'server-password', 'database', 3306, MYSQLI_CLIENT_SSL);
if (mysqli_connect_errno($conn)) {
die('Failed to connect to MySQL: '.mysqli_connect_error());
}
else{
echo "Connected";
}
Then I added in .env file following, beside defining username password host and db name.
BaltimoreCyberTrustRoot.crt.pem file is on same path as .env file
MYSQL_SSL_KEY=BaltimoreCyberTrustRoot.crt.pem
MYSQL_SSL_CERT=BaltimoreCyberTrustRoot.crt.pem
MYSQL_SSL_CA=BaltimoreCyberTrustRoot.crt.pem
MYSQL_SSL_CIPHER=BaltimoreCyberTrustRoot.crt.pem
MYSQL_SSL=true
In config/database.php looks like this
'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,
'options' => (env('MYSQL_SSL')) ? [
PDO::MYSQL_ATTR_SSL_KEY => env('MYSQL_SSL_KEY'), // /path/to/key.pem
PDO::MYSQL_ATTR_SSL_CERT => env('MYSQL_SSL_CERT'), // /path/to/cert.pem
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_SSL_CA'), // /path/to/ca.pem
PDO::MYSQL_ATTR_SSL_CIPHER => env('MYSQL_SSL_CIPHER')
] : [],
Not sure what is wrong, any idea?
I get these errors:
"error":"SQLSTATE[HY000] [2002] An attempt was made to access a socket in a way forbidden by its access permissions.
[{"file":"D:\home\site\wwwroot\vendor\laravel\framework\src\Illuminate\Database\Connection.php","line":624,"function":"runQueryCallback","class":"Illuminate\Database\Connection","type":"->","args":["select * from backend_token limit 1",[],{}]},
It helps to know which version of Laravel you are on to be absolutely sure, but on Laravel 8 my database config has:
'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'),
]) : [],
],
Don't change your database.php config but in your .env add MYSQL_ATTR_SSL_CA=/path/to/BaltimoreCyberTrustRoot.crt.pem
FYI sometimes if you are using private IP addresses then edit your hosts file
10.10.10.10 hostname.domain.com
Where of course the ip number matches the ip of the host.
If that doesn't work, add the cipher line from you config PDO::MYSQL_ATTR_SSL_CIPHER => env('MYSQL_SSL_CIPHER'),
I have secondary connection that comes dynamically and here is it's code
'userdb' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => DYNAMIC_DATABASE_NAME,
'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,
],
So, once use logged in I want to update update this database dynamically and reconnect to this userdb.
I have used following code but this is not giving persistent DB connection I have to this this everytime manually.
Any suggestions ?
DB::purge('userdb');
config(['database.connections.userdb.database' => 'test']);
DB::reconnect('userdb');
DB::connection('userdb');
1.first set database name
\Config::set('database.connections.userdb.database','test');
2.after set database name you need to purge the DB cache
\DB::purge('userdb');
3.set default connection after u purge db cache
\DB::setDefaultConnection('userdb');
I want to know about the laravel multiple databases. is it possible to use a default database which use only user login and after login separate group by group and every group use independent database. such as 'db' is the default database it's only for the all user login.
Example: Now 'John' is login using default database 'db'. John is the member of group1 after login john use 'db1' where stored John's all type of data. Other side Now 'Alex' login using default database 'db'. Alex is the member of group2 after login Alex use 'db2' where stored Alex's all type of data. After login default db connection no need so i want to replace 'bd' to 'db1' or 'db' to 'db2'. Please provide code for laravel
Define a separate database connection in config/database.php.
'mysql' => [ // default
'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', ''),
...
],
'db1' => [ // another
'driver' => 'mysql',
'host' => env('DB_HOST_ONE', '127.0.0.1'),
'port' => env('DB_PORT_ONE', '3306'),
'database' => env('DB_DATABASE_ONE', 'forge'),
'username' => env('DB_USERNAME_ONE', 'forge'),
'password' => env('DB_PASSWORD_ONE', ''),
...
]
Note that you have to define respective config in your .env.
Then when you want to use db1 connection it, use Config::set('database.default', 'db1'). However, it only works when you have known amount of database connections(that you can define in config/database.php), if you have unknown amount of databases, then you should change the config directly instead of the name of the connection only.
Example:
Config::set('database.connections.mysql.database', 'db1')
Config::set('database.connections.mysql.username', 'admin');
Config::set('database.connections.mysql.password', 'secret');
You can see my another answer to know how does it work underhood.
In this case, I presume you are building a system that is connecting to various existing databases possibly from different applications.
You can define as many DB connections as you want in your config/database.php
'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,
],
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST_2', '127.0.0.1'),
'port' => env('DB_PORT_2', '3306'),
'database' => env('DB_DATABASE_2', 'forge'),
'username' => env('DB_USERNAME_2', 'forge'),
'password' => env('DB_PASSWORD_2', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
...
You would however also need a way to specify for each user which other external DB from which their details are going to be fetched from, for instance a column db_name on the users table.
At the point of fetching the data of say 'Alex' User model, you can do something like
$user = User::find(1); //if Alex has user_id 1 and where $user->db_name is 'mysql2' as is in the config/database.php file
$userDetails = DB::connection($user->db_name)->where('username',$user->name)->where('other_details','some details')->get();
Make sure to specify this in your .env too, to match what's defined in config/database.php
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
DB_HOST_2=127.0.0.1
DB_PORT_2=3306
DB_DATABASE_2=db2_name_here
DB_USERNAME_2=db2_username_here
DB_PASSWORD_2=db2_password_here
...
Laravel throws the following error when I try to store records that contain 'ñ' and '´':
SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect string value: '\xE3\xB1es' for column 'names' at row 1 (SQL: insert into `clients` (`cedula`, `names`, `email`) values (5454545, Pablito Nu�es, email#dominio.com))
The database is in utf8_general_ci, as well as the html.
I used this mutator:
public function setNamesAttribute($value)
{
$this->attributes['names'] = strtr($value, 'àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ', 'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY');
}
But it stores it as "YA" instead of "n".
Any advice on how to solve this?
ps: I'm using laragon and laravel 5.4
Try utf8mb4 character set. Additionally you need to set connection charset to utf8mb4. So change 'charset' in config/database.php to 'utf8mb4'.
'charset' => 'utf8mb4',
For example:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_general_ci',
'prefix' => 'pre_',
'strict' => false,
'engine' => null,
],
I have API written in lumen(laravel). I am using Eloquent for my models.
What I need to do is to use different databases based on url (endpoint).
For example I have http://apiprovider.com/api/v1/ as base API url and it connects to the api_v1 database, but I need to use another database if v2 is used http://apiprovider.com/api/v2 for instance api_v2 database.
All classes and laravel application should be the same, only different database according to version.
Database settings is stored in .env file.
Please suggest the right way to implement this ? Or at least possible ways.
Thanks.
It's just an idea, i haven't tried it but a simple way would be switching between the two databases from a middleware.
For example you could define the two connections available in database.php :
'connections' => [
'mysql1' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST_2', 'localhost'),
'database' => env('DB_DATABASE_2', 'forge'),
'username' => env('DB_USERNAME_2', 'forge'),
'password' => env('DB_PASSWORD_2', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
]
Then create a middleware and switch the DB in the handle method
public function handle($request, Closure $next)
{
//check the request URL and decide what DB to use
//set the DB for this request
Config::set('database.default', $dbname );
}
I think that Config::set('database.default', $dbname); will work only for the current request, but it would do what you need