0
class CreateUsersTable extends Migration {

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

class CreateUsersExtendedTable extends Migration {
    
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::connection('mysql2')->create('users_extended', function(Blueprint $table ){
                $table->integer('user_id')->unsigned();
                $table->string('phone');
                $table->foreign('user_id')->references('id')->on('tweets.users')->onDelete('cascade');
            });
        }

Above is how i created a users_extended table in different database. What i want is to to bind my User Model to two tables which are in two different databases (tweets.users & tweets2.users_extended).

I want it because when ever i call User::save() It should enter records on both the related tables.

Currently I have to do this code for saving a user with extended information like phone:

public function store()
{
    $validation = Validator::make(Input::all(), User::$registerationRules);
    
    if($validation->fails())
    {
        return Redirect::back()->withInput()->withErrors($validation);
    }
    
    $user = new User();
    $user->email = Input::get('email');
    $user->password = Hash::make( Input::get('password') );
    $user->save();
    
    $userExtended = new UserExtended();
    $userExtended->phone = Input::get('phone');
    $userExtended->user()->associate($user);
    $userExtended->save();          
    
}

Please note that I am looking for a solution of having a single Model not Relationships.

I am new to MVC and as i have read blogs about it I didn't find a way to achieve this. May be someone here knows if there is any possibility.

Paul T.
  • 4,703
  • 11
  • 25
  • 29
Raheel
  • 8,716
  • 9
  • 60
  • 102
  • Check this http://stackoverflow.com/a/26336480/2166828 in the _contructor() the another model set the connection. – rigobcastro Jan 19 '15 at 07:44
  • so this mean i have to create a UserExtended Model and that should belongs to User Model? – Raheel Jan 19 '15 at 07:47
  • For example, model A belongs to db1, model B belongs to db2 and belongs to model B, normally in model B you say that belongs to model B via relation method, but you must say too the model B belongs to db2 in constructor. – rigobcastro Jan 19 '15 at 07:58
  • @rigobcastro Can you please provide me an example as per the code i have in question I did what was mentioned in the link you provided but still when i do `User:all()` i do not get the `phone` property. I have to use `$user->userExtended->phone` instead. – Raheel Jan 19 '15 at 09:00
  • Rephrase your question and describe the problem. You want separate connections for read/write operations or what? – Jarek Tkaczyk Jan 19 '15 at 10:35
  • @JarekTkaczyk Please see i have updated my question. The basic point is i have learned to make related models somehow but i feel it not correct having two models for a same User Entity. But the requriement is to place addtional user details in a separate table. – Raheel Jan 19 '15 at 11:25
  • 1
    So you don't need separate connections. All you want is table inheritance - read this: http://stackoverflow.com/a/26704488/784588 – Jarek Tkaczyk Jan 19 '15 at 11:39

0 Answers0