Laravel 5.7 artisan migrate - laravel-5.7

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();
});

Related

phpunit testable livewire fails with Undefined array key "fingerprint"

When trying a test that came with Laravel and Jetstream/Livewire libraries, I get an undefined array key "fingerprint" error message
Undefined array key "fingerprint"
at vendor/livewire/livewire/src/Testing/TestableLivewire.php:181
public function pretendWereSendingAComponentUpdateRequest($message, $payload)
{
$result = $this->callEndpoint('POST', '/livewire/message/'.$this->componentName, [
'fingerprint' => $this->payload['fingerprint'],
'serverMemo' => $this->payload['serverMemo'],
'updates' => [['type' => $message, 'payload' => $payload]],
]);
This happens for any out of the box feature tests that ship with Laravel9 with Jetstream when used against my project.
Here is one example that fails at the Livewire::test.... line.
The user is created and authenticating without issue and confirmed in other phpunit tests.
class BrowserSessionsTest extends TestCase
{
use RefreshDatabase;
public function test_other_browser_sessions_can_be_logged_out(): void
{
$this->actingAs($user = User::factory()->create());
Livewire::test(LogoutOtherBrowserSessionsForm::class)
->set('password', $user->password)
->call('logoutOtherBrowserSessions')
->assertSuccessful();
}
}
I stood up a fresh Laravel 9 project which works and began inserting various areas from my project into the fresh project as a way of hopefully identifying the issue. Session parameters, events, migrations, factories, models, were not the issue as it continued to work in the fresh project.
One thing I noticed is that the generic routes are not accepted in my project within he test cases. I have to insert 'https://realtor.host' in front of every test route (e.g. $response = $this->get('https://realtor.host/register');
I was curious if it was not evaluating the livewire route and I tried to add my domain into the vendor's livewire component in which the test still failed and that did not cause it to work.
Any ideas on where else I can look?

Migration error during creation of table in laravel

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.

Laravel Dusk testing with sqlite drops file busy exception

T try to write a laravel dusk login (with sqlite) test but I still kepp getting
Tests\Browser\LoginTest::employee_can_login
ErrorException: unlink(/var/www/laravel/database/database.sqlite): Text file busy
my test looks like
/** #test research analyst login **/
public function employee_can_login()
{
$user = factory(User::class)->create([
'email' => 'login#gmail.com',
'password' => bcrypt('password'),
]);
$this->browse(function (Browser $browser) use ($user) {
$browser->visit(new Login())
->type('email', 'login#gmail.com')
->type('password', 'password')
->press('Login')
->assertSee('Dashboard')
->assertSee('You are logged in!')
->assertSee($user->name);
});
}
on migration the sqlite tables were created,
What does that mean how do I fix this error?
As answered in similar thread, this is common problem (at least for me) with virtual machines. You can try not to share this file (so probably better move this file to not shared directory) or to solve this temporary you need to restart your virtual machine to make it work.

Limit languages in cms

I use silverstripe 3.1
I would like to limit the languages (To only German and English) which are available in the drop down in the CMS. Therefore I put
the following code in my mysite/_config.php
i18n::set_locale('de_DE');
$allowed_locales = array(
'de_DE' => array('Deutsch', 'Deutsch'),
'en_EN' => array('English', 'English')
);
i18n::$common_locales = $allowed_locales;
Afer a flush=1 i get the following error:
Fatal error: Cannot access private property i18n::$common_locales in ... _config.php
Any ideas what goes wrong?
Thank you
as of 3.1 most of the static php variables are private. this means you can no longer access those.
the reason for this api change is that they are now cached by the config layer (this is also why you have to ?flush=1 now after changing private statics in classes like for example with private static $db)
if you want to update something in the config layer, you can do this with:
Config::inst()->update('CLASS', 'FIELD', $value);
you could use use the config update to overwrite the common locales (class would be 'i18n', and field would be 'common_locales'):
Config::inst()->update('i18n', 'common_locales', $array);
Note: if you want to completely overwrite an existing configuration, you have to do a remove() first.
Config::inst()->remove('i18n', 'common_locales');
Config::inst()->update('i18n', 'common_locales', $array);
however, if you are using the translatable module and you want to limit the number of translatable languages, there is a much better way already built in:
// in your _config.php
i18n::set_locale('en_US');
Translatable::set_allowed_locales(array(
'de_DE',
'en_US',
));
Config it through YAML:
i18n:
common_locales:
nl_BE:
name: Dutch (Belgium)
native: Nederlands
fr_BE:
name: French (Belgium)
native: Francais

webdriver-test is unusable

On a virtual machine (clean, fresh Ubuntu server 11.04) I created a test website as described in Creating Your First Yii Application and now I want to create simple test using webdriver-test.
I set up proper TEST_BASE_URL in protected/tests/WebTestCase.php and created protected/tests/functional/MySimpleTest.php
<?php
Yii::import( 'ext.webdriver-bindings.CWebDriverTestCase' );
class MySimpleTest extends CWebDriverTestCase {
protected function setUp() {
parent::setUp( '192.168.57.1', 4444, 'firefox' );
}
public function testMySite() {
$this->get( TEST_BASE_URL );
$qElem = $this->findElementBy( LocatorStrategy::linkText, 'Users' );
$this->assertNotNull( $qElem, 'There is no "Users" link!' );
$qElem->clickAndWait();
$this->assertTrue( $this->isTextPresent( 'test1#example.com' ), 'The is no "test1#example.com" text on result page!' );
}
}
Running it looks like this:
etam#ubuntu:/var/www/test/protected/tests$ phpunit functional/MySimpleDbTest.php
PHPUnit 3.5.15 by Sebastian Bergmann.
E
Time: 5 seconds, Memory: 5.25Mb
There was 1 error:
1) MySimpleTest::testMySite
PHPUnit_Framework_Exception: setBrowserUrl() needs to be called before start().
/opt/yii-1.1.8.r3324/framework/test/CWebTestCase.php:61
/var/www/test/protected/extensions/webdriver-bindings/CWebDriverTestCase.php:156
FAILURES!
Tests: 1, Assertions: 0, Errors: 1.
Notice that it's complaining about setBrowserUrl() from PHPUnit_Extensions_SeleniumTestCase_Driver, which is not the same as one from CWebDriverTestCase.
I tried to find out what's going on, but it's too complicated to me. It looks like problems with old and new selenium API existing together, but I'm not sure about it.
I'm using:
Ubuntu server 11.04
yii 1.1.8.r3324
webdriver-test 1.1b
phpunit 3.5.15 (repaired as described in bugs.launchpad.net/ubuntu/+source/phpunit/+bug/701544)
Please help!
You need to call the setBrowseUrl() method right after the parent::setup() method because selenium requires this url to resolve relative paths on your test cases. So this way you could call open('full.url.com/someAction') or just open('/someAction') and both would go to the same page.

Resources