**users table:**

```
id    int primary key, auto increment
r_id  varchar unique
name  varchar

```

**books table:**

```
id       int primary key, auto increment
user_id  foreign key to r_id in users table
name     varchar

```

The relation in User Model:

```
return $this->hasMany('App\Book', 'user_id');

```

By default, `user_id` FOREIGN KEY is pointing to a PRIMARY KEY id. How can i make it pointing to a UNIQUE `r_id`? 

**If Only**, `r_id` is primary key, we can pass it with third argument and overriding `$primaryKey` property:

```
return $this->hasMany('App\Book', 'user_id','r_id');
protected $primaryKey = "r_id";

```