how to change database configs in laravel? - laravel

My larave app have setup steps.
In the database configuration step, user inputs should be placed as database configuration, but i dont know how can i set custom configs for database.
I used this code, but it changes the configs temporarily and doesn't save them.
Config::set("database.connections.mysql", [
"host" => "...host...",
"database" => "...database...",
"username" => "...username...",
"password" => "...password..."
]);

Just try to change .env file or on config/database.php do your corrections

Related

How to disable Redis in Laravel for dev version

I have a Laravel API that I need to test locally. How can I disable Redis cache on a local Laravel server? I can't disable it manually deleting all Redis::connection cases.
You can change "Default Cache Store" in config/cache.php file in your project.
By default you should have this 'default' => env('CACHE_DRIVER', 'file'),
You have a few solutions depending on what you have there:
if you already have changed default cache driver on that file, just change it to 'file' (not recommended!)
better would be change to default value 'default' => env('CACHE_DRIVER', 'file'), but add appropriate value in .env file CACHE_DRIVER=file

How to setup SQS to work using AWS Instance Profile instead of `key` and `secret`?

I am trying to setup SQS to use AWS Instance Profile instead of key and secret using Laravel 8.
I have tried the following in queue.php
'sqs' => [
'driver' => 'sqs',
'credentials' => CredentialProvider::memoize(CredentialProvider::instanceProfile()),
'prefix' => env('SQS_PREFIX', 'https://sqs.us-east-1.amazonaws.com/your-account-id'),
'queue' => env('SQS_QUEUE', 'your-queue-name'),
'region' => env('SQS_REGION', 'us-east-1'),
],
however, this gave me an error
Your configuration files are not serializable.
I know that removing the credentials key completely will by default force the sdk to use the default credentials provider, however, I am wondering if there is a way to use CredentialProvider::instanceProfile() instead since default credentials provider will look for keys in other places first then move on to Instance Profile role after the previous attempts failed (which I think is inefficient).
Can anyone help me with this?

How to change Laravel 5 sessions to redis

I now set a lot of values to the session using Session::put and Session::get. If I want to use Redis throughout the application, is it true that I just need to replace all Session:: with Redis:: to make it work?
By the way, I already installed the Redis package
All you have to do is just changing your .env file :
SESSION_DRIVER=redis
and use Session::get() etc.
You should change in connfig/session.php 'driver' => env('SESSION_DRIVER', 'file'), file to redis.

Persisting sessions across subdomains in Laravel 5

Using 5.0
in config/session.php I have set 'domain' => '.example.com' but it is not working. I cannot persist a session on even one domain like this.
My site has many subdomains:
vancouver.example.com
newyork.example.com
etc... they are hosted on the same server and are the same Laravel app (share the same storage directory)
I login with the correct credentials, upon which the app redirects to another page on the site, and I have no session at that point. var_dump(Auth::user()) shows null even though I logged in with the correct credentials.
storage/framework/sessions shows 14 different files there, they are all for me and I cleared them out before I started testing this.
I'll attach my AuthController#postLogin method below, which works fine if session.php 'domain' => null
public function postLogin(Request $request)
{
$this->validate($request, [
'email' => 'required|email', 'password' => 'required',
]);
$credentials = $request->only('email', 'password');
if ($this->auth->attempt($credentials, $request->has('remember'))) {
Session::flash('message', 'You are now logged in.');
Session::flash('status', 'success');
if (str_contains($_SERVER['HTTP_REFERER'], '?goto=')) {
$params = explode('?', $_SERVER['HTTP_REFERER'])[1];
$target = explode('=', $params)[1];
} else {
$target = '/';
}
return redirect($target);
}
return redirect($this->loginPath())
->withInput($request->only('email', 'remember'))
->withErrors([
'email' => $this->getFailedLoginMessage(),
]);
}
Figured it out. Update domain => '.example.com' in session.php and clear the cookies for the site in question.
#gadss
you need to add session table like this
php artisan session:table
composer dump-autoload
php artisan migrate
and change .env to
SESSION_DRIVER=database
also modify config/session.php
'driver' => env('SESSION_DRIVER', 'database') and
'domain' => '.yourdomain.com'
after that clear your browser's cache and cookies.
You'll need to update the session configuration to persist the session in domain-wide including subdomains. Follow the steps given below.
Go to config/session.php and update the domain with prefix . as config => '.your-domain.com'.
Then clear your application cache, Open the Chrome DevTool and Go to Application > Application > Clear Storage. You'll need to clear out the previous cookies also.
run artisan command php artisan config:cache or php artisan config:clear to drop previously cached laravel application configs.
If you are using database as the session driver, You need to create a session table for that. run command php artisan session:table to generate the session table migration and then migrate it using php artisan migrate. Then perform the three steps given above.
With Laravel 8 it becomes more simplier :
Add SESSION_DOMAIN to your .env file :
SESSION_DOMAIN=.yourdomain.tld
Clear configuration cache :
php artisan config:cache
Delete your browser sessions cookies, then session become shared between all your subdomains.
In my case I used to AutoLogin user to subdomain once account is created on www. domain. Worked fine.
Have you tried storing the sessions in the database, memcached, or redis instead of in files? I had a similar situation to yours and storing sessions in the database solved the issue for me.
For some reason Laravel's session driver doesn't handle cross domain sessions correctly when using the file driver.
If someone still gets the problem with subdomain cookie. Try to change Session Cookie Name in config/session.php
If someone needs to sync session in subdomains with different laravel application sharing same database
Follow all the instructions of #Kiran Maniya
Then you have to keep same application name in order to get same session name. Or just change the cookie config in config/session.php
You can hardcode it if keeping same name is not possible.
'cookie' => env(
'SESSION_COOKIE',
Str::slug(env('APP_NAME', 'laravel'), '_').'_session'
)
to something like:
'cookie' => env(
'SESSION_COOKIE',
'session_sharing_application_session'
)

How do you set default engine for the Schema Builder?

How can you set a default engine when using migrations (Schema Builder)? I recently got place on a shared hosting and their default MySQL engine is MyISAM. Instead of having to rewrite all my migration files to include $table->engine = 'InnoDB' I'm wondering if you can set this as default.
Is it possible?
I don't think it is,the docs http://laravel.com/docs/schema#storage-engines. Can't find any other mentioning of db engine in the docs.
You would expect it to be possible in app/config/database.php
I manage to do it. I'm using Laravel 5.5. In the config/database.php:
'connections' => [
'mysql' => [
...
'engine' => env('DB_ENGINE', null)
],
As you may know this will fetch the DB_ENGINE var from en .env file.
So in that file I setted like this:
DB_ENGINE=MyISAM
It will work if you set the var to InnoDB also.

Resources