4

So, I have a laravel web and have some tables, two of them are users and user_data. user_data have a foreign key user_id that references to id on users.

users have general attributes like username, email, password, etc. and user_data have 1 on 1 relation with users and have attributes like game_money, game_job, game_tier, etc. The columns are too long if I combine those 2 into 1 table, so I tought to normalize it.

User registration is working and running smooth. But I don't know how to add a new entry into user_data when a new user registered and add a new entry in users. The attributes columns in user_data (like game_money,etc.) are filled by external application outside laravel, so all I need is to add an entry to user_data.user_id foreign key, and let the other attributes in user_data use the default values or null before being updated by the external apps.

Here is my user_data migration:

Schema::create('user_data', function (Blueprint $table) {
            $table->unsignedBigInteger('user_id');
            $table->foreign('user_id')->references('id')->on('users');

            $table->string('account_tier')->default("Free");
            $table->float('economy')->default(0);
            $table->string('job')->nullable();

            $table->timestamps();
        });

Where should I put the insert query inside laravel? Or should I handle user_data using the external app?

Thank you very much.

Evan Gunawan
  • 387
  • 4
  • 17
  • You should make relationship between 2 table in Model first. After that, you can get, insert or even update 2 table easily. – Ngorld Jul 17 '19 at 04:38

4 Answers4

5

You should use an id on user_data table as your primary key. By the way you can just use the following code for your desired result. In the registration method use something like below:

$user = User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);

The user variable is holding the information of the newly created user. Now its time to insert into your user_data table. Assuming the model is UserData.

UserData::create([
            'user_id' => $user->id,
             ............
        ]);
zahid hasan emon
  • 6,023
  • 3
  • 16
  • 28
2

In RegisterController inside create function

$user= User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);
//UserData is the Model of user_data table
$user->UserData->create(['job'=>$data['job']]);
return $user

mohamed hassan
  • 479
  • 3
  • 6
  • What should i add inside the UserData model? Is it just need a `protected $table = 'user_data'` ? I don't want to add anything like fillable because I don't insert those value via the webpage. Thank You – Evan Gunawan Jul 17 '19 at 05:27
  • php artisna make:model UserData inside it add... protected $table = 'user_data' then protected $fillable=['account_tier','economy','user_id','job']; and make realtion with User and also in User model make the relation class with UserData – mohamed hassan Jul 17 '19 at 05:30
0

Try To This Example:

    use Illuminate\Support\Facades\DB;

    $user_data= array(
     'name' => $data['name'],
     'email' => $data['email'],
     'password' => Hash::make($data['password']),
    );
    $role_data= array(
     'user_name' => $data['name'],
     'email' => $data['email']
    );
    $response = DB::table('users')->insert($user_data);
    $response = DB::table('role')->insert($role_data);
0

You can use the Model boot events as following when you define relationship each other these models;

public static function boot()
{
    parent::boot();

    self::created(function($model){
        $model->userData()->create([
            'user_id' => $model->id,
            'job' => $model->job
        ]);
    });

}
Tuncay Elvanagac
  • 1,048
  • 11
  • 13