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,
],
Related
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
...
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.
Here is my query, it works on all older versions.
$data = DB::table('applicant')
->groupBy('AppAffID')
->get();
Error i get is :
SQLSTATE[42000]: Syntax error or access violation: 1055 'comloant_loantreeAPI.applicant.AppID' isn't in GROUP BY (SQL: select * from `applicant` group by `AppAffID`)
I have tried the database change :
I added 'strict' => false, to database config.
'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' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
What is it that you want to achieve with the groupBy? If you want to sort on the AppAffID you should use the orderBy instead done like so:
$data = DB::table('applicant')
->orderBy('AppAffID', 'desc') //can also use 'asc' for ascending
->get();
I'm using laravel 5. i need to get some data from a remote MySQL database.
i've already set up my database connection in config/database.php.
This is how it looks:
'connections' => [
'mysql' => [
'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,
],
'remotemysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '************'),
'database' => env('DB_DATABASE', 'osys'),
'username' => env('DB_USERNAME', 'Syn'),
'password' => env('DB_PASSWORD', '****************'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
],
The connection info is correct, i already tested it and i'm able to connect to the remote database.
To test it out i just fetched the database connection and the data in my controller to send it to a view to check whether or not everything is working alright. This is my controller:
...
use DB;
...
public function item()
{
$items = DB::connection('remotemysql')
->table('ip_products')
->get();
return view('admin.item', compact('items'));
}
and this is my view:
...
<tbody>
#foreach ($items as $item)
<tr>
<td>{{$item->id}}</td>
</tr>
#endforeach
</tbody>
...
When i try to load my view i get this error message:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'dvs.ip_products' >doesn't exist (SQL: select * from ip_products)
The error shows me that Laravel tries to get the table form the dvs database (wich is the main site database). So it isn't using the connection 'remotemysql'. If it couldn't connect to the remote database it would have got a connection error but i think its not using the remote connection at all.
What can i do to fix this issue ?
As You can see from error:
Table 'dvs.ip_products' >doesn't exist
where dvs is database name.
so according to Your config files:
'remotemysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '************'),
'database' => env('DB_DATABASE', 'osys'),
'username' => env('DB_USERNAME', 'Syn'),
'password' => env('DB_PASSWORD', '****************'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
env() method has 2 args: $key, $default
so it looks first to .env file to find $key in You case DB_HOST, DB_DATABASE... and if it's not defined will use $default that You define as second argument.
You can read about it here
when i try to execute DB query with 6 Joins i get following error message:
SQLSTATE[42000]: Syntax error or access violation: 1104 The SELECT would examine more than MAX_JOIN_SIZE rows; check your WHERE and use SET SQL_BIG_SELECTS=1 or SET MAX_JOIN_SIZE=# if the SELECT is okay
$results = DB::table('table_1')
->join('table_2', 'table_1.id', '=', 'table_2.id')
...
My question is:
How i can set "SET SQL_BIG_SELECTS=1" in laravel query?
i found a solution :)
in app/config/database.php
change following:
'mysql' => [
'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,
],
To:
'mysql' => [
'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' => '',
'options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET SESSION SQL_BIG_SELECTS=1',
),
'strict' => false,
],
I added the "options" array and define SQL_BIG_SELECTS
Thats it, we can configure PDO in any way we need!
Hope this is usefull to other ;)