I use Laravel 5.1 and a mysql server.
I have a database with table in utf8 unicode, and my databse configuration file is like this :
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => 'false',
),
But when I use queries from eloquent, my texts which have accents are broken. It's not a problem from blade because I can write accent in the views correctly, and if I use PDO directly my text is nice.
How can I solve my problem with my queries from eloquent ?
I found my problem, I hope it will help others :
My Mysql databases are encoded in latin1 by default. I was sure that my databases were in utf8 so I configured as that in the configuration file on my laravel project.
Related
I have a very specific situation regarding databases in Laravel.
We have two DIFFERENT servers, one with SQL SERVER and one MySQL.
The regular join (belongsTo, hasMany) between two different connections and different servers is working flawlessly and I can get all the data that I want.
The problem occurs when I want to add WHERE parameters to the relationship - Laravel will add subquery "and exists" into the query - which will, of course, fail because we have two different servers.
Both models have correction table and connection specified in model properties and as I said regular belongsTo and hasMany is returning the correct results from both servers. Only WHERE conditions are failing the query.
What are the ways to solve this problem and how do you usually deal with this?
Much appreciated!
I'm unsure if that's solving your problem but you could try the package laravel-cross-database-subqueries by hoyvoy.
Just extend your models with Hoyvoy\CrossDatabase\Eloquent\Model.
I had a similar requirement a while ago. The solution I settled on was to replicate the data via a cron job from the MSSQL server into my MYSQL server, but this was largely because the MSSQL server was extremely slow in comparison to the MYSQL server. This allowed me to query correctly.
Alternatively, if you don't want to replicate the data and can get good response times from the MSSQL server, then you may want to look at "temporary tables". If you query the MSSQL server first and then create a temporary table in the MYSQL server with this data to query against, this may suit as a work around.
If none of the above suit, you will need to query the two separately and use PHP to manipulate the data because you can't query MYSQL and MSSQL in the same query.
I see 2 options, the first one is duplicate the database, but you might run behind so perhapse not the best method.
The second option create a seperate query for the relationship filter and use the result of that query to fetch the data
First create connection on config/database.php
return [
'default' => 'first_db_connections',
'connections' => [
'first_db_connections' => [
'driver' => 'sqlsrv',
'host' => 'localhost',
'database' => 'database1',
'username' => 'user1',
'password' => 'pass1'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
'second_db_connection' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database2',
'username' => 'user2',
'password' => 'pass2'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
],
],
So your models use the normal default connection, then create model second connection to your models
class myModel extends Model {
protected $connection= 'second_db_connection';
protected $table = 'myTable';
public function post() {
return $this->belongsTo('App\Post');
}
}
I have a very, VERY weird issue. Our team has ignored this for a while because it hasn't broken anything (I know, not a good attitude), but I really want to know what the hell is/could be going on. We are on Laravel 5.2.
We have built very long queries using the laravel eloquent query builder. For some reason, sometimes, when we do a toSql(), we'll get the query we meant to run. When pasting the query, I'll get an error like this one:
Error Code: 1055. Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'onion.distance' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
Weird part though, changing nothing about it, laravel will run the query just fine! Anyone have ANY idea why laravel would run the query fine, but the toSql out put trigger this error? And how could we avoid it (other than manually adding the Group By's).
Not pasting code unless requested (lots of moving parts to some of the queries).
Thank you to Marcin Nabialek, you were 100% right. Laravel was overriding the default mysql server configuration and setting strict mode to false. For anyone that was confused as I was:
database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'dump_command_timeout' => 60 * 15, // 5 minute timeout
'unix_socket' => env('UNIX_SOCKET', ''),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'mysqltest'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
]
All I had to do was set strict to true
'strict' => true
In Laravel application I have a MySQL timestamp field deleted_at which was created for softDeletes and the default value is NULL. If the value is not NULL the Eloquent will not retrive the data. When I import CSV file the NULL value from source file becomes 0000-00-00 00:00:00.
I read Mysql import transforms timestamp null values to 0000-00-00 00:00:00 and \N does not worked for me. I have many tables which I need to import periodically, so I really need to fix this. Please help.
You need to disable strict mode in MySQL. Particularly, the mode name is STRICT_TRANS_TABLES . Open /etc/mysql/my.cnf (or the same file in Windows) find and comment out STRICT_TRANS_TABLES there.
More information is here:
http://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sql-mode-strict
I think you should see your config/database.php file. This file contains the variable from where you can set the strict mode - On / Off.
Laravel now gives you the power to enable / disable mysql modes. With this new feature, Laravel now has the ability to do three things: Disable "strict" mode, returning to the <= 5.6 behavior; enable "strict" mode, setting it to the 5.7 behavior; or customizing exactly which modes are enabled.
The code in your file goes like this...
'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' => true, // <----- This variable sets the strict mode.
'engine' => null,
],
set this value to false if you want to disable the strict mode else make it true to enable the strict mode.
Hope this help you to solve your problem.
I am in the process of migrating a cakephp 3.0 database from mysql to postgress. I used this tool for the database migration and it worked beautifully. After that I changed the config file as shown below.
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Postgres',
'persistent' => false,
'host' => 'localhost',
'port' => '5432',
'username' => 'postgres',
'password' => 'mypass',
'database' => 'cake_bookmarks',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
'log' => false,
'quoteIdentifiers' => false,
//'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
],
The root folder in localhost also shows "successfully connected to database". However when i run my application, it shows an error:
Cannot describe mytable. It has 0 columns. Cake\Database\Exception
I can't make sure if this is because of not connecting to the database (which i think is unlikely as the root page shows as connected) or cakephp being unable to use my database.
If so, how can I fix the issue. I am quite new to cakephp too, just confguring and doing basic stuff.
Try the following (test after each step):
Check if the table is present in the database
Check if the expected columns are defined into the table
Clear the Cake cache (if is FileCache is enough to delete files under tmp/cache/persistent tmp/cache/models and tmp/cache/views
Check the permissions of the specific user on the database cake_bookmarks (maybe via phppgadmin)
Hope to help!
This is becoming a really annoying problem. I am following the Quick setup directions on laravel.com. I have followed all the steps until I have to go:
php artisan migrate
The result is:
[PDOException]
SQLSTATE[42000] [1049] Unknown database 'database'
migrate [--bench[="..."]] [--database[="..."]] [--path[="..."]] [--package[="..."]] [--pretend] [--seed]
And I go back and check the database.php file and it reads:
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
Afterwards, I go to localhost:8000/users and it displays:
PDOException
SQLSTATE[42000] [1049] Unknown database 'database'
open: /Users/benamorgan/Sites/run-away-world/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php
*/
public function createConnection($dsn, array $config, array $options)
{
$username = array_get($config, 'username');
$password = array_get($config, 'password');
return new PDO($dsn, $username, $password, $options);
}
I have been trying to change the database name, provide a root password, start and stop mysql, change the naming in every possible way for database, change localhost to 127.0.0.1. I don't know what to do, hence I'm stopping by here to ask where I messed up.
Laravel doesn't create databases for you, you need to do it on your own. Log in to mysql and create your database, then provide the name in your db config.
You can do it quickly in your console:
echo "create database my_database" | mysql -u root -p
then in your config change the database name:
'database' => 'my_database',
To add, make sure that you are using the MySQL driver in the config file, although this must be obvious.
again make sure the database in MySQL is created prior and you are using the right credentials (User, Key)