0

I have a users table with two types 'student' or 'faculty' in type column. I would like to create two different tables from the user table for faculty and students...

I thought of creating two models for faculty and students but i can't think ahead of that on how to populate tables for these models.

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration {

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('users', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('name');
        $table->string('identif')->unique();
        $table->string('type');
        $table->string('email')->unique();
        $table->string('password', 60);
        $table->rememberToken();
        $table->timestamps();

        //Add Primary Key

    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('users');
}     }

1 Answer 1

1

The easiest way would be to just run a raw query and copy data from users table to the other 2 tables, if you're using MySQL then sth like the following could work:

DB::statement("INSERT INTO students (name, identif, email, password) SELECT (name, identif, email, password) FROM users WHERE type = ?", array('student'));

Other databases should offer similar feature.

The above is ok if you do not need Eloquent model logic being run for those records. Otherwise just fetch User objects, create new Student or Faculty objects and save the new objects:

Users::all()->map(function($user) {
  if ($user->type == 'student') {
    Student::create($user->toArray());
  } else {
    Faculty::create($user->toArray());
  }
});

If you want a new User of Faculty object created every time a Users object is created, you can use Eloquent model events:

//User.php
protected static function boot() {
  parent::boot();

  static::created(function($user) {
    if ($user->type == 'student') {
      Student::create($user->toArray());
    } else {
      Faculty::create($user->toArray());
    }
  });
}
Sign up to request clarification or add additional context in comments.

5 Comments

unfortunately I need eloquent model logic being run on those records. @jedrzej.kurylo
Then the only option is to just fetch all users and create new eloquent objects for students and faculties. You can do that in the migration file.
Oh wow! How can i accomplish that? :D i want that whenever there is a new entry in users table... depending on its type student or faculty..it should be automatically added to respective table... can we do it from laravel itself.. ? :)
Hmm, why would you need that? I thought you want to get rid of users table. Why do you want 2 copies of user data in your database? One in users and one in student/faculty.
Anyway, I updated the answer to show how to create Student/Faculty every time User is created.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.