Migrate database problem in laravel 5.7.16 - laravel

I am new in Laravel. I am facing a problem to migrate my users and password reset table.
Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or
access violation: 1071 Specified key was too long; max key length is
767 bytes (SQL: a lter table users add unique
users_email_unique(email))
I have tried so many solution like (AppServiceProvider, database collation change, changing migration file ) but none of them give me a solution.
AppServiceProvider.php
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
Schema::defaultStringLength(191);
}
}
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' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
],

After setting defaultStringLength in AppServiceProvider maybe re-run your project with php artisan serve solve your problem.

Related

Connection Error with Laravel Passport and PostgreSQL

I am installing a new API with Laravel Passport using POSTGRESQL databases. When installing Laravel and basic authentication, it works ok to the point of registering users and logging in perfectly. (take the database ok, this is important!) But when following the steps to install Passport and run the "php artisan migrate" command, Passport does not recognize that I am using POSTGRESQL and looks for the connection to MYSQL, which is non-existent. I will appreciate ideas, I don't know what else to do.
ERROR:
Illuminate\Database\QueryException
SQLSTATE[HY000] [1045] Access denied for user 'forge'#'localhost' (using password: NO) (SQL:
create table `oauth_auth_codes` (`id` varchar(100) not null, `user_id` bigint unsigned not
null, `client_id` bigint unsigned not null, `scopes` text null, `revoked` tinyint(1) not
null, `expires_at` datetime null) default character set utf8mb4 collate
'utf8mb4_unicode_ci')
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:671
667| // If an exception occurs when attempting to run a query, we'll format the
error
668| // message to include the bindings with SQL, which will make this exception a
669| // lot more helpful to the developer instead of just the database's errors.
670| catch (Exception $e) {
671| throw new QueryException(
672| $query, $this->prepareBindings($bindings), $e
673| );
674| }
675|
I try without result:
php artisan cache:clear
php artisan config:cache
My .ENV:
#DB_CONNECTION=mysql
#DB_HOST=127.0.0.1
#DB_PORT=3306
#DB_DATABASE=laravel
#DB_USERNAME=root
#DB_PASSWORD=
My CONFIG/DATABASE WITH POSTGRESQL:
'default' => env('DB_CONNECTION', 'pgsql'),
'connections' => [
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'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' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'pgsql' => [
'driver' => 'pgsql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'xxxx'),
'username' => env('DB_USERNAME', 'xxxxxxxxxxx'),
'password' => env('DB_PASSWORD', 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'schema' => 'someschema',
'sslmode' => 'prefer',
],
I finally found the solution in a Laravel Passport forum, it seems that in the latest versions the connection to the db is configured directly in passport (I don't know why it doesn't take the one from .env or config / databases), "publishing" (exporting) from the I sell the connection file that includes the Passport Storage Driver to the app, to be able to modify it and declare the connection exclusively for Passport. Needless to say, there are 3 different places in Laravel to declare a connection to the database, subject to chatting directly with Otwell.
Anyway, this is done with the following command:
php artisan vendor:publish --tag=passport-config
which adds a file in the config folder called passport.php, and where we find, ready to configure:
/*
|--------------------------------------------------------------------------
| Passport Storage Driver
|--------------------------------------------------------------------------
|
| This configuration options determines the storage driver that will
| be used to store Passport's data. In addition, you may set any
| custom options as needed by the particular driver you choose.
|
*/
'storage' => [
'database' => [
'connection' => env('DB_CONNECTION', 'mysql'),
],
],

Route Model Binding on different database

I have multiple databases and as a model considers default connection when invoked from a route model binding, it says table not found. How can i bind the model in a resource controller edit/update method as a part of Route Model binding to consider the current connection and not the default connection?
PS - I cannot declare $connection on the model because, i won't know which database it will be accessing at a said moment.
You can use a route middleware to set the database connection for particular routes. To do this you will need to set up your connections in your database config, and then add the middleware.
I use a similar setup in a multi-tenant application to manage system vs tenant database connections. Database config:
'connections' => [
'system' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'lms'),
'username' => env('DB_USERNAME', 'lms'),
'password' => env('DB_PASSWORD', 'lms'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'tenant' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => '',
'username' => '',
'password' => '',
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
],
The middleware in my example enforced the use of a database defined as tenant.
use Closure;
use Illuminate\Support\Facades\Config;
class EnforceTenancy
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
Config::set('database.default', 'tenant');
return $next($request);
}
}
You can then register the middleware in the kernel.php $routeMiddleware array
'tenancy.enforce' => \App\Http\Middleware\EnforceTenancy::class,
and then apply the middleware in your route file for any route that requires the second db.

Laravel: Jobs failed - SQLSTATE[42S02]: Base table or view not found

I'm developing a Multi Tenant with Laravel v5.7 and I'm successful in sending queue emails, since my models have the property 'connection' defined.
But when trying to send, for example, an email using the Jobs class, the same fails and informs that the table of model does not exist.
From what error recorded in the table 'failed_jobs', even with the property 'connection' defined, it appears that the Job nevertheless tries to connect to the main database and not to the specified database of the property.
Is there any way to specify in Job which database to use, since the same is informed in the model?
database.php
'connections' => [
'others' => ['...']
'TENANT001' => [
'driver' => 'mysql',
'database' => env('TENANT001_DATABASE', ''),
'host' => env('TENANT001_HOSTNAME', ''),
'port' => env('DB_PORT', '3306'),
'username' => env('TENANT001_USERNAME', 'forge'),
'password' => env('TENANT001_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
],
Sample Model
class Template extends Model
{
/**
* The database name used by the model.
*
* #var string
*/
protected $connection = 'TENANT001';
}
failed_jobs
PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'main_database.templates' doesn't exist in /www/samba/laravel.local/vendor/laravel/framework/src/Illuminate/Database/Connection.php:326
I guess you are trying to access second connection TENANT001 in Template Model.
class Template extends Model
{
/**
* The database name used by the model.
*
* #var string
*/
protected $connection = 'TENANT001';
protected $table='templates';
}
After that in your config/database.php
'connections' => [
'others' => ['...']
'TENANT001' => [
'driver' => 'mysql',
'database' => env('TENANT001_DATABASE', ''),
'host' => env('TENANT001_HOSTNAME', ''),
'port' => env('DB_PORT', '3306'),
'username' => env('TENANT001_USERNAME', 'forge'),
'password' => env('TENANT001_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
Or you can use connection in the queue.php
'database' => [
'connection' => 'TENANT001'
'driver' => 'mysql',
'table' => 'jobs',
'queue' => 'default',
'expire' => 60,
],
And clear the cache once you update .env files.
php artisan config:cache
And check you have define everything correctly in .env and table exists.
Thanks

Two database connect in laravel

How to connect two database in laravel-5 and how to get data from db.
I know two one thing for that
In config/database set like this.
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'larashop'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'larashop2'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Using Query you can define a connection on the Query Builder:
$users = DB::connection('mysql2')->select('your query');
Using Eloquent
You can also define which connection to use in your Eloquent models as well!
<?php
class SomeModel extends Eloquent {
protected $connection = 'mysql2';
}
You can also define the connection at runtime via the setConnection method.
<?php
class SomeController extends BaseController {
public function someMethod()
{
$someModel = new SomeModel;
$someModel->setConnection('mysql2');
$something = $someModel->find(1);
return $something;
}
}
I have find the issue of connect 2 database in laravel.
If any body wants two work with 2 database then config as par above suggestion and remove database configuration from .env file which is located at root.
I have tried and I am working with 2 database.

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