Laravel Migrations gives unusual error - laravel-4

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

Related

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

Password_resets table missing, even after doing php artisan migrate

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

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.

Syntax error or access violation:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an
error in your SQL syntax; check the manual that corresponds to your
MariaDB server version for the right s yntax to use near '&beauty
CHANGE id id INT AUTO_INCREMENT NOT NULL' at line 1
Hi there,
i have met a problem in my laravel project, when i tried to edit my column by migration.
I just need to change length of 'id' in table.
Here is my code`
class FieldChange extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('health&beauty', function (Blueprint $table) {
$table->integer('id', 11)->change();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
}
}
You have to use single `` for your table name like this.
`health&beauty`

neo4j migration in laravel doesnot work properly

I am using Neo4j in my application with /Vinelab/NeoEloquent. Now the problem is that when I run php artisan neo4j:migrate --database=neo4j
, node for the migration table is created.
Not any labels defined in function up() is not created. What should I do to solve this?
Schema class is,
<?php
use Vinelab\NeoEloquent\Schema\Blueprint;
use Vinelab\NeoEloquent\Migrations\Migration;
class CreateTestpostsTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Neo4jSchema::label('Testposts', function(Blueprint $label)
{
$label->unique('tpid');
$label->index('name');
$label->index('content');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Neo4jSchema::drop('Testposts');
}
}
According to your Schema code, no labels should be expected to be created. You are setting a constraint on the tpid to be unique, and indexing the name and content of the nodes with Testposts label.
To check if your migration has taken effect:
Try creating two nodes with the same tpid and you should get an error
Try profiling the queries to the name and content properties, this link should help with that: http://neo4j.com/docs/stable/cypherdoc-basic-query-tuning-example.html
On a side note, if the Label of the node in your model is Post or anything other than Testposts, then you should change Testposts to whatever is in your model for this to work properly.

Resources