They're just examples, hasOne describes exactly what it does - it returns ONE related object, and belongsTo is the inverse of the relationship. In the example, a User hasOne Phone and a Phone belongsTo a User.
If you wanted the user to have more than one phone, you would use hasMany which would then return an object collection (of Phones) when queried upon.
Take some time to learn basic php, a few months, then come back to laravel.
Also SQL. Learn some stored procedures, that will help you understand how to write a query.
The SQL generated by hasOne
is actually
select * from phones where user_id = 1 limit 1
The limit 1
ensures that only one record is returned.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community