riten
1 year ago

Relations issue, how to make it correct?

Posted 1 year ago by riten

I'm trying to connect users table with user_info. I've found some posts about it and it works, but I've troubles with user_info->id autoincrement.

If I set it to: $table->increments('id');

I'm getting error that no default value is set, it works only if I add $table->integer('id')->nullable($value = true);

Here is what I got for now:

User model

public function userinfo()
{
    return $this->hasOne('App\Userinfo');
}

Userinfo model

public function user()
{
    return $this->belongsTo('App\User');
}

Migration/table structure

    public function up()
        {
            Schema::create('user_info', function (Blueprint $table) {
                //There is a problem, if I don't add user_id it says that user_id doesnt exists, so I created user_id and decided to set id to autoincrement but it doesnt works so I made it like that 
                $table->integer('id')->nullable($value = true);
                $table->foreign('id')->references('id')->on('users');
                $table->integer('user_id');
                
$table->string('avatar',255)->default('notset.jpg')->nullable($value = true);
                $table->string('looking_for',255)->nullable($value = true);
                $table->string('steam_nick',40)->nullable($value = true);
                $table->integer('age')->length(3)->nullable($value = true);
                $table->integer('isFriendly')->length(2)->nullable($value = true);
                $table->integer('isToxic')->length(2)->nullable($value = true);
                $table->integer('isLeader')->length(2)->nullable($value = true);
                $table->integer('isFunny')->length(2)->nullable($value = true);
                $table->integer('isSkilled')->length(2)->nullable($value = true);
                $table->text('description')->nullable($value = true);
                $table->timestamps();
            });
        }

Finally UserinfoController

 public function update(Request $request, $id)
 {
      $user = User::findOrFail($id); //Get role specified by id


       $this->validate($request, [
           'steam_nick'=>'max:120',
           'looking_for'=>'required',
           'avatar'=>'',
           'description'=>'max:450',
           'age'=>'min:2'
       ]);
       $input = $request->only(['steam_nick', 'looking_for', 'description','avatar','age']); //Retreive the name, email and password fields
        
       $userinfo = new Userinfo();
       if ($userinfo->user_id == null ) {
           $userinfo->steam_nick = $request->steam_nick;
           $userinfo->looking_for = $request->looking_for;
           $userinfo->description = $request->description;
           $userinfo->avatar = $request->avatar;
           $userinfo->age = $request->age;
           $user->Userinfo()->save($userinfo);
       } else {
           $user->Userinfo()->update($input);
       }
      
    }

Phpmyadmin img

All I need is to set id in user_info same as id in users. I don't need user_id, but couldn't make it works other way, but I'm sure it's wrong.

Please sign in or create an account to participate in this conversation.