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

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.

Related

Laravel Issue with Artisan when using different computer on NAS to run terminal

I have my code on a local NAS at home. When I run
php artisan serve
on terminal in my desktop computer, the application starts up no problem.
However, when I am on my laptop, and I want to run the server, the following part just runs endlessly and doesn't proceed:..
Starting Laravel development server: http://127.0.0.1:8000
The reason I'm asking here is I am having a hard time knowing how to troubleshoot this issue.
The error I'm receiving when I try to load the app URL from my laptop while the terminal is still in 'starting' mode is..
SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from `sessions` where `id` = ZbOqp13BZNRquGtmG0Vi2XNHGWi3WDMJxW4ZYYCK limit 1)```
Also, my database.php in the config folder under mysql looks like this:
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '33060'),
'database' => env('DB_DATABASE', 'homestead'),
'username' => env('DB_USERNAME', 'homestead'),
'password' => env('DB_PASSWORD', 'secret'),
'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'),
]) : [],
],```
I don't see this issue mentioned on SO previously, and would appreciate ideas on how to troubleshoot this. Ideally I should be able to connect to the server fine even if I am on another computer in the same network.
Troubleshooting tips appreciated.
My .env for the database is:
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret```

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

Laravel Database charset and collation

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.

Artisan migrate in Laravel 5 throwing PDOException

I just created a fresh Laravel 5 application and wrote some migrations. Whenever I tried to run php artisan migrate, I encountered the following error
[PDOException]
SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '
/var/lib/mysql/mysql.sock' (13)
My database configuration is correct
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'), //I also tried 127.0.0.1
'database' => env('DB_DATABASE', 'mydb'),
'username' => env('DB_USERNAME', 'myusername'),
'password' => env('DB_PASSWORD', 'mypassword'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
I am using LAMP stack, could this be the problem for Laravel 5?
You can modify the database config (database.php) to this
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost,
'database' => 'YOUR DB NAME',
'username' => 'YOUR DB USERNAME',
'password' => 'YOUR DB PASSWORD,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Or you can set the up the .env file
Every Laravel app now ships with a default .env.example file, which at the moment looks like this:
APP_ENV=local
APP_KEY=SomeRandomString
DB_DATABASE=your_db_name
DB_USERNAME=your_username
DB_PASSWORD=your_db_password
In order to use this file, just copy it and name the copy (new file) .env and enter your database details.
The .env file should be in the root folder like .env.example.
Note:- don't rename the .env.example file....always create a new .env
The env('DB_DATABASE', 'default_value') takes the value of DB_DATABASE from .env file if not found selects the default 'default_value'.
Since the config is using env() you need to set the values in the .env file found in the root of your project. The values you are passing as the second parameter are defaults, which will only be used when the specific key is not found in the .env file. So the database portion of .env would look like this:
DB_HOST=localhost
DB_DATABASE=mydb
DB_USERNAME=myusername
DB_PASSWORD=mypassword
You can read more on how Laravel handles enviroments in the Configuration Docs.

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