Laravel Database charset and collation - laravel

In Laravel, Everytime, while running the website,
"set names 'utf8' collate 'utf8_unicode_ci'"
is executing.
How can I avoid this ?
Instead of doing this in the code. We have configured in the database itself.
How can I remove charset and collation in database configuration ?
> config/database.php
>
> 'database' => [
> 'driver' => 'mysql',
> 'host' => env('DB_HOST', 'localhost'),
> 'port' => env('DB_PORT'),
> 'database' => env('DATABASE'),
> 'username' => env('DB_USERNAME', 'forge'),
> 'password' => env('DB_PASSWORD', ''),
> 'charset' => 'utf8', // Need to remove or make ''
> 'collation' => 'utf8_unicode_ci', // Need to remove or make ''
> 'prefix' => env('DB_PREFIX', ''),
> 'strict' => false, ],

You can just remove 'charset' and 'collation' from mysql connection array and run php artisan config:clear after that.

Not directly sure what you try to achieve with this. But it is not possible without changing the MySQL Classes.
Take a look at the Illuminate/Database/Connectors/MySqlConnector Class.

Related

Dynamic Database Connection Using Laravel

I am working on a new application that must connect to several databases. The databases are dynamically created when a new company is registered in my application i.e each company have there on databases.
My question is how to establish the database connection using laravel,
If the company admin is login he needs to connect the corresponding db. How is it possible ???
I know how to establish multiple database connection if the databases are stable, like,
'mysql1' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge1'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
...
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge2'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
...
but if the databases are not stable how is it possible ????
If my question is not up to standard, please forgive me
You can add dynamically the database configuration like this.
if(NULL === config()->get('database.connections.company')){
config()->set('database.connections.company', [
'driver' => $companyDbDriver,
'host' => $companyDbHost,
'port' => $companyDbPort,
'database' => $companyDbName,
'username' => $companyDbUser,
'password' => $companyDbPassword,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false
]);
}
in the model; set the connection attribute to 'company'
/**
* Connection configuration ID.
*
* #var string
*/
protected $connection = 'company';
there are complication if you dont load the configuration of the database before calling the models using it, i'll leave that to you.
First you have to know where your database connections parameters are stored.
Anyway, wherever they are, you should create a new connection on the fly.
Supposing each User has it's own connection ...
<?php
use Illuminate\Support\Facades\Config;
$user = Users::find($id);
Config::set('database.connections.' . $user->username, array(
'driver' => 'mysql',
'host' => $user->db_host,
'database' => $user->db_name,
'username' => $user->db_user,
'password' => $user->db_pass,
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
'prefix' => '',
));
// And set the new connection to interested models
$myModel = new MyModel;
$myModel->setConnection($user->username);
$data = $myModel->where(...)->get();
Hope this help

Work with mysql database in Laravel 4.2

I am new to Laravel used this https://github.com/msurguy/laravel-facebook-login link to work with facebook login in Laravel. I have setup all the things but i
got following errors when establishing database connection :
Type error: Argument 1 passed to
Illuminate\Redis\Database::__construct() must be of the type array,
null given, called in
C:\wamp64\www\laravel-facebook-login-master\vendor\laravel\framework\src\Illuminate\Redis\RedisServiceProvider.php
on line 23
I want to run simple mysql database. Below is my database.php code:
return array(
'fetch' => PDO::FETCH_CLASS,
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'facebookdb',
'username' => 'root',
'password' => '',
'port' => '3603',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
),
'migrations' => 'migrations',
);
Any suggestion/help will be highly appreciated.
You are missing the definition for your redis connection in that connections array, something like this will help:
'redis' => array(
'host' => env('REDIS_HOST', 127.0.0.1),
'port' => env('REDIS_PORT', 6379),
'password' => env('REDIS_PASSWORD', null),
'database' => env('REDIS_DATABASE', 0)
)
Make sure that you add the corresponding entries in your .env file if your redis connection settings differ from the default values above.

Google Cloud app engine with Laravel: unix_socket issue

I am using Laravel version 5.1.*. Here is my db config file...
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'username',
'password' => 'password',
'unix_socket' => '/cloudsql/zoho-portal-159018:us-central1:zoho-portal',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
it gives error No such file or directory. It seems, unix_socket is the issue. But exact same config works when I use normal PDO connect without laravel....
$DBH_SOCKET = '/cloudsql/zoho-portal-159018:us-central1:zoho-portal';
$DBH_NAME = 'DBH_NAME';
$cns="mysql:unix_socket=".$DBH_SOCKET.";dbname=".$DBH_NAME.";charset=utf8";
$user='user';
$password='password';
try{
$DBH = new PDO($cns,$user,$password);
$DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
If It works in normal single php file without laravel... then it must should work in laravel way. But not not working.. Any comments highly appreciated. Thanks

vendor/config.php has database settings, how do I regenerate this?

I have a Laravel 5 project that I copied to a new folder. I changed the database settings in the .env file. But now I am concluding that database settings are also set in vendor/config.php. What command must I run to have this regenerated? I already tried composer dump-autoload and composer update without success
The default database config is in config/database.php and looks in .env for its settings (with some fallback defaults). For example, the default MySQL connection:
'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,
],
Laravel does not have a vendor/config.php file.

Laravel define database in model

A chunk of my SQL is held in a different database to the rest of my Laravel installation.
When I use a particular Model, how do I define in that Model, that I'd like to use a particular database and not the one defined in config/database.php?
I am using Laravel 5 so needs to be relevant to that version.
Update: Here is my model;
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Lookup extends Model {
protected $connection = 'postcodes';
}
Here is part of my config/database.php;
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', $_ENV["DB_HOST"]),
'database' => env('DB_DATABASE', $_ENV["DB_DATABASE"]),
'username' => env('DB_USERNAME', $_ENV["DB_USERNAME"]),
'password' => env('DB_PASSWORD', $_ENV["DB_PASSWORD"]),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'postcodes' => [
'driver' => 'mysql',
'host' => env('DB_HOST', $_ENV["DB_HOST"]),
'database' => env('DB_DATABASE', 'postcodes'),
'username' => env('DB_USERNAME', $_ENV["DB_USERNAME"]),
'password' => env('DB_PASSWORD', $_ENV["DB_PASSWORD"]),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
It's not working - and I can't get Whooops errors to work so all I get is a white screen (but that's a separate issue).
I know my controller code is correct because when I temporarily copy the table in to my main Laravel database, it all works fine. So what's the issue?
You can define protected $connection = 'yourohterconnection'; in your model, and add another connection to your database config file.
I asked the same question on the Laravel forum and the posted solution worked for me.
Basically the problem is this line;
'database' => env('DB_DATABASE', $_ENV["DB_DATABASE_TWO"]),
Or in my code above at the time of writing it was actually;
'database' => env('DB_DATABASE', 'postcodes'),
It needs to be;
'database' => env('DB_DATABASE_TWO', $_ENV["DB_DATABASE_TWO"]),
And then your .env file needs to have DB_DATABASE_TWO=postcodes (or whatever the name of your second database is).

Resources