Laravel one to one relationship with example

Solomon Eseme
Backend Developers
Published in
8 min readDec 3, 2020

--

“Laravel eloquent one to one relationship with example”: A one-to-one relationship is a very basic, fundamental Laravel eloquent relationship.

Following our complete guide to Laravel relationships, it simply relates one entity to another.

For instance, in our voting application, a User is associated with one Vote.

If we were to describe this relationship, we could say:

Each user has one vote”.

Before we dive into the article, subscribe to get access to our free laravel tips that will improve your productivity.

Defining a one-to-one relationship

In order to define this relationship, we call the hasOne method on the entity that owns the relationship.

Here’s a code snippet defining the relationship.

<?phpnamespace App\Models;use Illuminate\Database\Eloquent\Model;class User extends Model{/** * Get's this user's vote */ public function vote() {    return $this->hasOne(App\Models\Vote::class); }

--

--

Solomon Eseme
Backend Developers

A Software Engineer who’s geared towards building high performing and innovative products. I help you become a great Backend Engineer @ masteringbackend.com