Migration error during creation of table in laravel - laravel-5.7

I'm using xampp as server in my laravel project. While creating migrations, I'm facing an error: only one migration table can be created; others are not being created.
This is the error:
**Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_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 `users` add unique `users_email_unique`(`email`))
at C:\xampp\htdocs\lsapp\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll format the error
661| // message to include the bindings with SQL, which will make this exception a
662| // lot more helpful to the developer instead of just
the database's errors.
663| catch (Exception $e) {
> 664| throw new QueryException(
665| $query, $this->prepareBindings($bindings), $e 666| );
667| }
668|
Exception trace:
1 PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes")
C:\xampp\htdocs\lsapp\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458
2 PDOStatement::execute()
C:\xampp\htdocs\lsapp\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458**

Update your database.php file in the config folder as show below
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
to
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
and it should work. If it doesn't work then update AppServiceProvider.php in app/Providers/ as shown below.
use Illuminate\Support\Facades\Schema; //Import Schema
function boot()
{
Schema::defaultStringLength(191); //Solved by increasing StringLength
}

You can try this:
AppServiceProvider.php
use Illuminate\Support\Facades\Schema; //Import Schema
function boot()
{
Schema::defaultStringLength(191);
}
Hope that helps.

Related

Wordpress plugin makes website error: crayon-syntax-highlighter

When I enable the plug-in "crayon-syntax-highlighter" version 2.8.4, my website shows nothing in each page.
---Error message:----
Fatal error: Uncaught Error: Call to a member function id() on array in
/var/www/it/wp-content/plugins/
crayon-syntax-highlighter/crayon_formatter.class.php:36
Try to modify this file : crayon_langs.class.php
File location: /wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php
Going to line# 340
[Before]
return preg_replace('/[^\w-+#]/msi', '', $id);
Need to add a '' behind w
[After]
return preg_replace('/[^\w\-+#]/msi', '', $id);
refer to https://crunchify.com/fix-for-crayon-syntax-highlighter-wordpress-plugin-and-php-7-3-fatal-error

Silverstripe 4, How to solve database table conflict that maps to same database table

So I made a mistake and accidentally defined two classes that lead to the same database table
<?php
namespace Sca {
use SilverStripe\ORM\DataObject;
class Gallery extends DataObject {
private static $table_name = 'Gallery';
// ...
}
}
And the other class
<?php
namespace Sca {
use SilverStripe\ORM\DataObject;
class GalleryHolder extends DataObject {
private static $table_name = 'Gallery';
// ...
}
}
Then I ran /dev/build and the site crashed. Now the only thin I see is the apache error on apache log under /var/log/apache/..
[2019-08-05 23:36:32] error-log.ERROR: Uncaught Exception
LogicException: "Multiple classes ("Sca\GalleryHolder", "Sca\Gallery")
map to the same table: "Gallery"" at
/var/www/demo/sc2/vendor/silverstripe/framework/src/ORM/DataObjectSchema.php
line 299 {"exception":"[object] (LogicException(code: 0): Multiple
classes (\"Sca\GalleryHolder\", \"Sca\Gallery\") map to the same
table: \"Gallery\" at
/var/www/demo/sc2/vendor/silverstripe/framework/src/ORM/DataObjectSchema.php:299)"}
[]
I dumped the database and $ grep'ed the dirs with no success to find the place where there is defined GalleryHolder that leads to creating Gallery table for that class. No results. I also deleted the class file for GalleryHolder and ran again /dev/build but still gas that error and "Server error" screen when visiting from the web.
Is there any suggestions where to clear the cache or schema to resolve the conflict?
So I found a quick fix. I went to /var/www/demo/sc2/vendor/silverstripe/framework/src/ORM/DataObjectSchema.php:299
and commented out the lines that was throwing an error and added new line that modifies the table name if there is conflict. I reset changed these lines
if ($conflict) {
// throw new LogicException(
// "Multiple classes (\"{$class}\", \"{$conflict}\") map to the same table: \"{$table}\""
// );
$table = $table . '_dupl';
}
Then I was able to load the page again and it rebuilt the cache by it self and I uncomented previousley commented out lines at /var/www/demo/sc2/vendor/silverstripe/framework/src/ORM/DataObjectSchema.php:299 .

mpdf temporary file directory is not writable when upload in server

When I run the file on my localhost it works, but when i upload it to my server using winSCP, I'm getting this error
PHP Fatal error: Uncaught Mpdf\MpdfException: Temporary files directory "E:\Inetpub\vhosts\gsm.org.my\httpdocs\print/custom/temp/dir/path" is not writable in E:\Inetpub\vhosts\gsm.org.my\httpdocs\print\vendor\mpdf\mpdf\src\Cache.php:17
Stack trace:
#1 E:\Inetpub\vhosts\gsm.org.my\httpdocs\print\vendor\mpdf\mpdf\src\Mpdf.php(1054): Mpdf\ServiceFactory->getServices(Object(Mpdf\Mpdf), Object(Psr\Log\NullLogger), Array, 0, Object(Mpdf\Language\LanguageToFont), Object(Mpdf\Language\ScriptToLanguage), NULL, NULL, NULL, NULL)
#2 E:\Inetpub\vhosts\gsm.org.my\httpdocs\print\print-form.php(88): Mpdf\Mpdf->__construct(Array)
#3 {main} thrown in E:\Inetpub\vhosts\gsm.org.my\httpdocs\print\vendor\mpdf\mpdf\src\Cache.php on line 17
Is it because the server couldn't find the file path or I wrote it wrong?
I tried giving permission on folder src but it said cannot change properties of file src. I am a beginner in this field. I tried search the solution on google regarding this error but I couldn't find anything.
In case you want to give mPDF another try:
it seems you are not providing the proper configuration to mPDF but we can't tell for sure as that part of your code (line 88 of your print-form.php) is missing. Taken from my last code using mPDF:
try {
$mpdf = new \Mpdf\Mpdf([
'tempDir' => __DIR__ . '/../tmp', // uses the current directory's parent "tmp" subfolder
'setAutoTopMargin' => 'stretch',
'setAutoBottomMargin' => 'stretch'
]);
} catch (\Mpdf\MpdfException $e) {
print "Creating an mPDF object failed with" . $e->getMessage();
}
Line 17 in Cache.php is part of the Cache constructor and throws an error in case the temporary directory is either not writable or not a directory:
// taken from method "createBasePath($basePath)"
if (!is_writable($basePath) || !is_dir($basePath)) {
return false;
}
To test if you see the error due to insufficient file permissions or a non-existing directory, upload a file with this content to your server and navigate to it with your preferred browser:
<?php
$pathToCheck= "E:\\Inetpub\\vhosts\\gsm.org.my\\httpdocs\\print//custom//temp//dir//path";
print 'Folder exists: '.(is_dir($pathToCheck) ? 'yes' : 'no').'<br />';
print 'Folder is writable: '.(is_writable($pathToCheck) ? 'yes' : 'no').'<br />';
You are on a Windows server so you will need to add the right user to the "tmp" folder under "Properties"->"Security", additionally check if the folder has the Attribute "Read-only" unticked.
Additional advice:
Please post relevant code in your future question (like the relevant portions of your print-form.php) as this reduces the risks of having to guess what might be the cause.

Laravel 5.7 artisan migrate

I installed recently laravel 5.7. How to fix error "php artisan migrate"?
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_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 `users` add unique `users_email_unique`(`email`))
at C:\laravel\blg2\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll format the error 661| // message to include the bindings with SQL, which will make this exception a 662| // lot more helpful to the developer instead of just the database's errors. 663| catch (Exception $e) {
> 664| throw new QueryException(
665| $query, $this->prepareBindings($bindings), $e
666| );
667| }
668|
Exception trace:
1 PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes") C:\laravel\blg2\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458
2 PDOStatement::execute()
C:\laravel\blg2\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458
Please use the argument -v to see more details.
Thank you
Laravel uses the utf8mb4 character set by default, which includes support for storing "emojis" in the database. If you are running a version of MySQL older than the 5.7.7 release or MariaDB older than the 10.2.2 release, you may need to manually configure the default string length generated by migrations in order for MySQL to create indexes for them.
Inside your AppServiceProvider:
use Illuminate\Support\Facades\Schema;
...
public function boot()
{
Schema::defaultStringLength(191);
}
Alternatively, you may enable the innodb_large_prefix option for your database. Refer to your database's documentation for instructions on how to properly enable this option.
For more information about indexes: https://laravel.com/docs/5.7/migrations#indexes
If you are running a version of MySQL older than the 5.7.7 release or MariaDB older than the 10.2.2 release, you may need to manually configure the default string length generated by migrations in order for MySQL to create indexes for them.
You can have it fixed as Chin Leung said.
Or adding the length of the record directly in the migration.
$table->string('email', 100); VARCHAR equivalent column with a optional length.
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 50);
$table->string('email', 90)->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
On xampp php 7.3.2, laravel 5.7 & also in laravel 5.8.3 it happened to me. Only changed the config/database.php file.
config/database.php
Changed charset & collation on mysql & it worked fine. Change this part
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
as
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
Now it will accept the long keys.
You need to go to
App\Providers\AppServiceProvider.php
Now Add the following code and then save and run php artisan serve
use Illuminate\Support\Facades\Schema;
...
public function boot()
{
Schema::defaultStringLength(191);
}
I use Laravel 5.7 but this error always comes up. ADD this code in every project. Also try to use Laravel with php7+ so that it doesnot show you PDO::Exception Error...
Open your AppServiceProvider.php, Path >> 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()
{
**Schema::defaultStringLength(191);**
}
You can define the string length as well to solve this kind of error
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name',20);
$table->string('email',80)->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password',8);
$table->rememberToken();
$table->timestamps();
});

Doctrine DBAL and sqlite problems

I am trying to connect to sqlite database file with Doctrine DBAL.
<?php
use Doctrine\DBAL\DriverManager;
require_once 'bootstrap.php';
$connectionParams = [
'url' => 'sqlite:///crawls.db',
];
$conn = DriverManager::getConnection($connectionParams);
But when I try to execute sql code it says that table is not exist (of course I checked manually and it is there).
$conn->exec('SELECT * FROM crawl_item');
outputs
PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1 no such table: crawl_item' in /home/px/Documents/phpcrawler/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:57
Stack trace:...
May be this output can be helpful
var_dump($conn->connect());
var_dump($conn->getDatabase());
bool(true)
NULL
If you look at the AbstractSQLiteDriver::_constructPdoDsn() method you will see that the parameter is 'path':
$connectionParams = [
[
'driver' => 'pdo_sqlite',
'path' => '../products.db'
]
);
$conn = DriverManager::getConnection($connectionParams);
greetings,
thomas

Resources