Password_resets table missing, even after doing php artisan migrate - laravel

I am trying to do password recovery in laravel, but after inserting an email to send the reset request, an error appears saying that password_resets doesn't exist.
I've already tried to migrate again but nothing works.
SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "password_resets" does not exist
LINE 1: delete from "password_resets" where "email" = $1 ^ (SQL: delete from "password_resets" where "email" = blabla#gmail.com)

According to this, it seems that the command to generate the migration for password_resets no longer exists, you can try to create a new migration with this:
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
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')->index();
$table->timestamp('created_at');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('password_resets');
}
}

In my case, I fixed the issue of the missing table by adding these two lines in AppServiceProvider.php.
Follow these steps:-
Open AppServiceProvider.php (Location: app/Providers/AppServiceProvider.php).
Add this line outside from classes use Illuminate\Support\Facades\Schema;
Add this line inside function boot() Schema::defaultStringLength(191);
Delete all tables from database.
Run this command php artisan migrate
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
Schema::defaultStringLength(191);
}
}

Related

SQLSTATE[42804] Error when trying to heroku run php artisan migrate

I've been trying to heroku run php artisan migrate from AWS Cloud9 without any success. How can I fix this problem? I would appreciate it very much if someone could help me. Thanks a lot!
I get three errors, all of which are saying
SQLSTATE[42804]: Datatype mismatch: 7 ERROR: column "administered" cannot be cast automatically to type smallint
HINT: You might need to specify "USING administered::smallint"
The migration file in question is as follows:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class ChangeFilariasisMedicationsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('filariasis_medications', function (Blueprint $table) {
$table->dropColumn('administered');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('filariasis_medications', function (Blueprint $table) {
$table->smallInteger ('administered');
});
}
}
Here is the related model.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
// 追加
use App\AdministeredDate;
use App\User;
use Illuminate\Notifications\Notifiable;
use App\Notifications\ReminderMail;
class FilariasisMedication extends Model
{
protected $fillable = [
'start_date',
'number_of_times',
'counter',
'administered'
];
protected $dates = [
'start_date',
];
use Notifiable;
/**
* この投薬スケジュールを所有するユーザ(Userモデルとの関係を定義)
*/
public function medication_user()
{
return $this->belongsTo(User::class);
}
/**
* この投薬スケジュールに属する投薬確定日(AdministeredDateモデルとの関係を定義)
*/
public function administered_date()
{
return $this->hasMany(AdministeredDate::class);
}
}
Current description of filariasis_medications table:
mysql> describe filariasis_medications;
enter image description here
You put a space before the parenthesis in your migration, remove it.
from $table->smallInteger ('administered');
to $table->smallInteger('administered');

Why won't artisan allow me to create data from my model?

I'm rather new to Laravel and am currently running Laravel 8.x on Windows 10. I've created a database table, model, factory and seeder for generating articles for an imaginary blog. (I'm imitating a tutorial on Laravel and Vue that I like on YouTube and also this article).
The article suggests verifying that everything works by going into php artisan tinker and executing the command:
Article::factory()->create();
When I do that, I get this message:
PHP Fatal Error: Class 'Article' not found in Psy Shell code on line 1
I have no idea what that's supposed to mean but I assume it's not happy with my code in some respect. I've looked at it as thoroughly as I can but don't see anything wrong based on the examples in the article. Could someone more knowledgeable in Laravel kindly eyeball this and tell me what I've done wrong? (For what it's worth, I tried User::factory()->create(); and it worked fine.)
Article.php is stored in app/Models and contains:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
use HasFactory;
protected $fillable = [
'title', 'body',
];
}
ArticleFactory.php is stored at database/factories and contains:
<?php
namespace Database\Factories;
use App\Models\Article;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class ArticleFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* #var string
*/
protected $model = Article::class;
/**
* Define the model's default state.
*
* #return array
*/
public function definition()
{
return [
'title' => $this->faker->text(50),
'body' => $this->faker->text(200)
];
}
}
2020_11_30_034856_create_articles_table is stored at database/migrations and contains:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateArticlesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('articles');
}
}
Lastly, ArticlesTableSeeder.php is stored at database/seeders and contains:
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class ArticlesTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Article::factory()
->times(30)
->create();
}
}
Can anyone see why this code isn't working?
EDIT:
I tried Patricus' suggestion and it worked fine in tinker. However, when I tried php artisan db:seed, it failed. I got this error:
Seeding: Database\Seeders\ArticlesTableSeeder
Error
Class 'Database\Seeders\Article' not found
at C:\Laravel\larticles\database\seeders\ArticlesTableSeeder.php:16
12▕ * #return void
13▕ */
14▕ public function run()
15▕ {
➜ 16▕ Article::factory()
17▕ ->times(30)
18▕ ->create();
19▕ }
20▕ }
1 C:\Laravel\larticles\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:36
Database\Seeders\ArticlesTableSeeder::run()
2 C:\Laravel\larticles\vendor\laravel\framework\src\Illuminate\Container\Util.php:40
Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
Tinker doesn't run in a namespace. While it tries to help you out with some things related to models, it can't autocorrect namespaces on static method calls.
Your model is \App\Models\Article. In tinker, you need to call:
\App\Models\Article::factory()->create();

Laravel does not create a record in `migrations` table on refresh

I have an awkward problem, every second time I run php artisan migrate:refresh --seed the migrations table is empty, though it should have initial migration record added after migration: 2018_05_02_114819_add_initial_migration
Here is my migration code (it is an import of old database schema):
<?php
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddInitialMigration extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
if(!Schema::hasTable('users')){
DB::unprepared(File::get(database_path('sam.sql')));
}
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
DB::statement('SET FOREIGN_KEY_CHECKS=0');
foreach (DB::select('SHOW TABLES') as $table) {
$table_array = get_object_vars($table);
if($table_array[key($table_array)] !== 'migrations'){
DB::statement('DROP TABLE ' . $table_array[key($table_array)]);
}
}
DB::statement('SET FOREIGN_KEY_CHECKS=1');
}
}
I found it out. It seems that in .sql file there was a line that said:
SET AUTOCOMMIT = 0;
Which was not set back to 1 in the end of the file.

MorphMany laravel relation does not work for some reason

I am using laravel 5.6 version, here is my Test model file
<?php
namespace App\Model\data\Models;
use Illuminate\database\Eloquent\Model;
class Test extends Model
{
protected $guarded = ['id'];
protected $table = 'tests';
/*
* Test - TestTranslation relation
* Test has many translations
*/
public function translations()
{
return $this->hasMany('App\Model\Data\Models\TestTranslation');
}
/*
* Test - Image relation
* Test can have an thumbnail
*/
public function thumbnails()
{
return $this->morphMany('App\Model\Data\Models\Image', 'type');
}
}
Here is my Image model
<?php
namespace App\Model\Data\Models;
use Illuminate\Database\Eloquent\Model;
class Image extends Model
{
protected $guarded = ['id'];
protected $table = 'images';
/*
* Image belongs to Post, Account, Comment
*/
public function type()
{
return $this->morphTo();
}
}
Here is my Database scheme for Images table:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateImagesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('images', function (Blueprint $table) {
$table->increments('id');
$table->string('path');
$table->enum('type', ['avatar', 'thumbnail', 'generic'])->default('generic');
$table->integer('type_id')->unsigned()->nullable();
$table->enum('type_type',[
"Account",
"Comment",
"Post",
"Skill",
"Test"
])->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('images');
}
}
In AppServiceProvider I linked 'Test' to my model file:
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
use Laravel\Dusk\DuskServiceProvider;
use Illuminate\Database\Eloquent\Relations\Relation;
use App\Model\Data\Models\Account;
use App\Model\Data\Models\EmailChangeConfirmation;
use App\Model\Observers\Common\AccountObserver;
use App\Model\Observers\Common\EmailChangeConfirmationObserver;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
Schema::defaultStringLength(191);
Relation::morphMap([
'Account' => 'App\Model\Data\Models\Account',
'Comment' => 'App\Model\Data\Models\Comment',
'Post' => 'App\Model\Data\Models\Post',
'Skill' => 'App\Model\Data\Models\Skill',
'Test' => 'App\Model\Data\Models\Test'
]);
if(env('APP_ENV') != 'testing') {
Account::observe(AccountObserver::class);
EmailChangeConfirmation::observe(EmailChangeConfirmationObserver::class);
}
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
if ($this->app->environment('local', 'testing')) {
$this->app->register(DuskServiceProvider::class);
}
}
}
As you can see, I have more morhpMany types, and all of them works fine. But for some reason, this Test relation that I added recently always returns an empty collection, even tough I am sure that test has thumbnails. Cause:
in this screenshot, I have records in database that each test has two images. Their ID matches ids of created tests and type is "Test". I tried to clear all caches, run composer dump-autoload but none of them helped.
Maybe someone can help identify what is wrong here? Because as I said before other types as Post, Account works fine.

Laravel Migrations gives unusual error

I am trying to create a migration using artisan. The migrations class is created without any problems and I construct the table using the Schema class. When I run its. It works all good, but when I try and rollback, It gives an error which I don't understand.
My Migration Class
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class Session extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('Session', function(Blueprint $table){
$table->string('id', 50);
$table->string('email', 100);
$table->integer('lastActivity');
$table->primary('id');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('Session');
}
}
Error message
[2014-03-25 14:42:16] production.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Class 'Session' not found' in E:\Documents\Dropbox\Documents\WorkSpace_netBeans\Laravel_Test\vendor\laravel\framework\src\Illuminate\Database\Migrations\Migrator.php:297
Your Session migration class might be in conflict with Laravel's Session (Facade), rename it to test.
I just created one here:
php artisan migrate:make Session
And it conflicted while trying to rollback:
PHP Fatal error: Call to undefined method Illuminate\Support\Facades\Session::down() in

Resources