Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Integrity constraint violation: 1452 Cannot add
- or update a child row: a foreign key constraint fails (`twitter`.
- `tweets`, CONSTRAINT `tweets_user_id_foreign` FOREIGN KEY (`user_
- id`) REFERENCES `users` (`id`) ON DELETE CASCADE)
- <?php
- use IlluminateSupportFacadesSchema;
- use IlluminateDatabaseSchemaBlueprint;
- use IlluminateDatabaseMigrationsMigration;
- class CreateTweetsTable extends Migration
- {
- /**
- * Run the migrations.
- *
- * @return void
- */
- public function up()
- {
- Schema::create('tweets', function (Blueprint $table) {
- $table->increments('id');
- $table->integer('user_id')->unsigned()->index();
- $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
- $table->string('body', 140);
- $table->timestamps();
- });
- }
- /**
- * Reverse the migrations.
- *
- * @return void
- */
- public function down()
- {
- Schema::dropIfExists('tweets');
- }
- }
- <?php
- namespace App;
- use IlluminateDatabaseEloquentModel;
- class Tweet extends Model
- {
- protected $fillable = [
- 'user_id', 'body',
- ];
- }
- <?php
- use AppTweet;
- use IlluminateDatabaseSeeder;
- class TweetsTableSeeder extends Seeder
- {
- /**
- * Run the database seeds.
- *
- * @return void
- */
- public function run()
- {
- factory(Tweet::class, 10)->create([
- 'user_id' => 2
- ]);
- }
- }
- <?php
- /*
- |--------------------------------------------------------------------------
- | Model Factories
- |--------------------------------------------------------------------------
- |
- | Here you may define all of your model factories. Model factories give
- | you a convenient way to create models for testing and seeding your
- | database. Just tell the factory how a default model should look.
- |
- */
- /** @var IlluminateDatabaseEloquentFactory $factory */
- $factory->define(AppUser::class, function (FakerGenerator $faker) {
- static $password;
- return [
- 'name' => $faker->name,
- 'email' => $faker->unique()->safeEmail,
- 'password' => $password ?: $password = bcrypt('secret'),
- 'remember_token' => str_random(10),
- ];
- });
- $factory->define(AppTweet::class, function (FakerGenerator $faker) {
- return [
- 'body' => $faker->realText(140),
- ];
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement