Errors in migration when installing the Laravel Voyager package - laravel

i have i brand new installation of Laravel 5.5, but when i try to install the Voyager admin panel i have this error :
[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter table `translations` add unique `translati
ons_table_name_column_name_foreign_key_locale_unique`(`table_name`, `column_name`, `foreign_key`, `locale`))
Config
PHP version : 7.0.10
MYSQL version : 5.7.14
CODE UPDATE
I think i have found the concerned code :
Schema::create('translations', function (Blueprint $table) {
$table->increments('id');
$table->string('table_name');
$table->string('column_name');
$table->integer('foreign_key')->unsigned();
$table->string('locale');
$table->text('value');
$table->unique(['table_name', 'column_name', 'foreign_key', 'locale']); // SOURCE OF THE ERROR ?
});

You need to Update the "config/database.php" for 'mysql'.
'engine' => null
To
'engine' => 'InnoDB ROW_FORMAT=DYNAMIC',
also Update the "app/Providers/AppServiceProvider.php"
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
// Specified key was too long error, Laravel News post:
Schema::defaultStringLength(191);
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
}
and run these commands in your project folder.
php artisan cache:clear
php artisan config:clear
php artisan voyager:install --with-dummy

The total length of your index is too long.
The column on which the unique index is added shouldn't be such long like VARCHAR columns, because the index will be very bulky and inefficient.

This is a known issue.
If you do not use the muli-language features from Voyager simply uncomment the line in *******************_create_translations_table
// $table->unique(['table_name', 'column_name', 'foreign_key', 'locale']);
or try to update your MySql Database to 7.x

Related

How to index table column of specific database with code in laravel

I want to write code to index the column of a specific database table column.
Iam trying in this way
DB::connection('mysql2')->raw("ALTER TABLE `consignments` DROP INDEX customer_reference");
What is correct method for this process
You need to use the Laravel migrations, see this documentation:
Laravel Migrations
Laravel Migrations/Index related
You can execute this command in the shell on the project path and edit the created file in the database/migration path and add the index for the column following the documentation
php artisan make:migration add_index_to_consignments_table
Or create manually a file in the database/migrations path with a name like this:
2021_07_29_022532_add_index_to_consignments_table.php and copy/paste this php content
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddIndexToConsignmentsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up(): void
{
Schema::table('consignments', function (Blueprint $table) {
//Uncomment one line depending on what you need
//$table->index('customer_reference'); //If you need to add an index
//$table->dropIndex('name_of_index_on_table'); //If you need to remove an index
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down(): void
{
Schema::table('consignments', function (Blueprint $table) {
//Uncomment one line depending on what you need
//$table->dropIndex('name_of_index_on_table'); //If you need to add an index
//$table->index('customer_reference'); //If you need to remove an index
});
}
}
After creating the file with any method you have to execute this command in the shell on the project path:
php artisan migrate

Laravel & PostGres - Migration down() will not drop table (TimeScaleDB Extension)

Here is what create_facts_table looks like:
class CreateFactsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::connection('pgsql')->create('facts', function (Blueprint $table) {
$table->bigInteger('entry_id');
$table->string('tag');
$table->timestampTz('time');
$table->double('sensor_value');
$table->index(['entry_id', 'tag', 'time']);
$table->unique(['entry_id', 'tag', 'time']);
});
DB::connection('pgsql')->statement('SELECT create_hypertable(\'facts\', \'time\');');
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
// Tried both, neither would delete the table
Schema::connection('pgsql')->dropIfExists('facts');
//DB::connection('pgsql')->statement('DROP TABLE facts;');
}
}
I don't get any error from down(). I am able to login as the DB user specified in my .env file and run DROP TABLE facts.
When I run php artisan migration:fresh up() fails to create the table and throws a duplicate table error:
Duplicate table: 7 ERROR: relation "facts" already exists
After manually deleting the table, I can then run php artisan migration:fresh. I must specify connection('pgsql') as I'm using multiple databases. Semi-unrelated, but I'm using TimeScaleDB extension (hence create_hypertable())
I dug a bit deeper on this recently. I hadn't directly mentioned I was using the TimeScaleDB extension ('SELECT create_hypertable(\'facts\', \'time\');').
I updated the title of the question incase someone lands here from Google in the future.
Here is what I have learned:
When php artisan migration:fresh is run, it attempts to drop all tables in batch and does not use the down() method. TimeScaleDB hypertables cannot be deleted in batch.
The solution is to use php artisan migration:refresh instead which will run the defined drop() operations for each table.
Source: https://stackoverflow.com/a/69105447/5449796

Issues when running php artisan migrate tables from laravel app to localhost database

The following: Is the error I would receive (Mind you if it's not the users table, it's the failed_jobs or reset_password table. The majority of the issues comes from the laravel auto-generated tables. And yes I did try PHP artisan migrate: reset, drop the tables from the localhost, I've even dropped the database itself)
Migrating: 2014_10_12_100000_create_password_resets_table
Illuminate\Database\QueryException
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `password_resets` add index `password_resets_email_index`(`email`))
at C:\xampp\htdocs\project1\project\seed\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|
1 C:\xampp\htdocs\project1\project\seed\vendor\laravel\framework\src\Illuminate\Database\Connection.php:464
PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes")
2 C:\xampp\htdocs\project1\project\seed\vendor\laravel\framework\src\Illuminate\Database\Connection.php:464
PDOStatement::execute()
And this is what the database/migration/create_reset_password_table looks like so far.
class CreatePasswordResetsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('password_resets', function (Blueprint $table) {
$table->string('email')->index();
$table->string('token');
$table->timestamp('created_at')->nullable();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('password_resets');
}
}
Thank you in advance for all the help! :)
Laravel default collation type has changed to utf8mb4.
In your
AppServiceProvider.php
boot method the following line
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}

UUID issue with passport Laravel

I want to use UUID in Laravel passport instead of the default id in my laravel project.
I changed all the user_id and client_id columns to uuid in the migrations and i add Passport::ignoreMigrations() in the register method in AppServiceProvider.
When i tested in postman by creating a new raw in users table, i get in my response the token but when i use this token in a secured route i always get response status:
401 Unauthorized and in the response body message:Unauthenticated.
things I did to correct this problem:
in AppServiceProvider and register function add:
Passport::ignoreMigrations()
run php artisan vendor:publish --tag=passport-migrations
in CreateOauthAuthCodesTable change to
$table->uuid('id');
$table->primary('id');
$table->uuid('user_id')->index();
so on other passport tables and migrations
in appServiceProvider and boot method add below code:
use Laravel\Passport\Client;
use Ramsey\Uuid\Uuid;
Client::creating(function (Client $client) {
$client->incrementing = false;
$client->id = \Ramsey\Uuid\Uuid::uuid4()->toString();
});
Client::retrieved(function (Client $client) {
$client->incrementing = false;
});
then I migrate my tables with php artisan migrate:fresh (Alert: This command will delete all current tables of database)
the key note is don't forget to add
protected $primaryKey = 'Id'; // in my case I capitalize id to Id
there you are!
You must change the user_id in oauth_access_tokens to uuid type, follow this steps:
Create one migration to alter the table oauth_access_tokens:
php artisan make:migration alter_table_oauth_access_tokens
public function up()
{
Schema::table('oauth_access_tokens', function (Blueprint $table) {
$table->dropColumn('user_id');
});
Schema::table('oauth_access_tokens', function (Blueprint $table) {
$table->uuid('user_id')->index()->nullable();
});
}
php artisan passport:install --uuids

ErrorException running php artisan vendor:publish with Elqouent-Sluggable

I'm using Laravel 5 and attempting to install Eloquent-Sluggable. I have followed all the steps in the installation instructions:
composer require cviebrock/eloquent-sluggable
composer update
Add Cviebrock\EloquentSluggable\SluggableServiceProvider::class to the Providers in config/app.php
php artisan vendor:publish
The last step resulted in this error message:
[ErrorException] Argument 2 passed to
Cviebrock\EloquentSluggable\SluggableTableCommand::__construct() must be an instance of Illuminate\Foundation\Composer, instance of
Illuminate\Support\Composer given, called in
C:\wamp\www\blog\vendor\cvi
ebrock\eloquent-sluggable\src\SluggableServiceProvider.php on line 92 and defined
What caused the error to occur?
It happened to me too. This is because the SluggableTableCommand requires Composer instance in its constructor but accidentally different type of instance is passed.
This might not be the smartest way to fix this but if you are in rush you can just remove the Composer declaration before $composer in the SluggableTableCommand constructor. The file that you need to edit is vendor/cviebrock/eloquent-sluggable/src/SluggableTableCommand.php on line 44
This should work:
/**
* Create a new migration sluggable instance.
*
* #param SluggableMigrationCreator $creator
* #param Composer $composer
*/
public function __construct(
SluggableMigrationCreator $creator,
$composer
) {
parent::__construct();
$this->creator = $creator;
$this->composer = $composer;
}
I had the same error using Laravel 5.2.29 when I tried to create migration via console command
php artisan sluggable:table posts slug
What solved the issue was creation usual Laravel migration, but I had commented 'Cviebrock\EloquentSluggable\SluggableServiceProvider::class' row in config/app.php file before.
php artisan make:migration add_slug_to_posts_table
with the next code in migration file
public function up(){
Schema::table('posts', function(Blueprint $table){
$table->string('slug')->nullable();
});
}
public function down(){
Schema::table('posts', function(Blueprint $table){
$table->dropColumn('slug');
});
}
and ran the migration
php artisan migrate

Resources