@@ -114,66 +114,82 @@ public function getTable()
114114 }
115115
116116 /**
117- * Define a one-to-one relationship.
118- *
119- * @param string $related
120- * @param string $foreignKey
121- * @return \Illuminate\Database\Eloquent\Relations\HasOne
122- */
123- public function hasOne ($ related , $ foreignKey = null )
117+ * Define a one-to-one relationship.
118+ *
119+ * @param string $related
120+ * @param string $foreignKey
121+ * @param string $localKey
122+ * @return \Illuminate\Database\Eloquent\Relations\HasOne
123+ */
124+ public function hasOne ($ related , $ foreignKey = null , $ localKey = null )
124125 {
125126 $ foreignKey = $ foreignKey ?: $ this ->getForeignKey ();
126127
127128 $ instance = new $ related ;
128129
129- return new HasOne ($ instance ->newQuery (), $ this , $ foreignKey );
130+ $ localKey = $ localKey ?: $ this ->getKeyName ();
131+
132+ return new HasOne ($ instance ->newQuery (), $ this , $ foreignKey , $ localKey );
130133 }
131134
132135 /**
133- * Define a one-to-many relationship.
134- *
135- * @param string $related
136- * @param string $foreignKey
137- * @return \Illuminate\Database\Eloquent\Relations\HasMany
138- */
139- public function hasMany ($ related , $ foreignKey = null )
136+ * Define a one-to-many relationship.
137+ *
138+ * @param string $related
139+ * @param string $foreignKey
140+ * @param string $localKey
141+ * @return \Illuminate\Database\Eloquent\Relations\HasMany
142+ */
143+ public function hasMany ($ related , $ foreignKey = null , $ localKey = null )
140144 {
141145 $ foreignKey = $ foreignKey ?: $ this ->getForeignKey ();
142146
143147 $ instance = new $ related ;
144148
145- return new HasMany ($ instance ->newQuery (), $ this , $ foreignKey );
149+ $ localKey = $ localKey ?: $ this ->getKeyName ();
150+
151+ return new HasMany ($ instance ->newQuery (), $ this , $ foreignKey , $ localKey );
146152 }
147153
148154 /**
149- * Define an inverse one-to-one or many relationship.
150- *
151- * @param string $related
152- * @param string $foreignKey
153- * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
154- */
155- public function belongsTo ($ related , $ foreignKey = null )
155+ * Define an inverse one-to-one or many relationship.
156+ *
157+ * @param string $related
158+ * @param string $foreignKey
159+ * @param string $otherKey
160+ * @param string $relation
161+ * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
162+ */
163+ public function belongsTo ($ related , $ foreignKey = null , $ otherKey = null , $ relation = null )
156164 {
157- list (, $ caller ) = debug_backtrace (false );
165+ // If no relation name was given, we will use this debug backtrace to extract
166+ // the calling method's name and use that as the relationship name as most
167+ // of the time this will be what we desire to use for the relatinoships.
168+ if (is_null ($ relation ))
169+ {
170+ list (, $ caller ) = debug_backtrace (false );
171+
172+ $ relation = $ caller ['function ' ];
173+ }
158174
159175 // If no foreign key was supplied, we can use a backtrace to guess the proper
160176 // foreign key name by using the name of the relationship function, which
161177 // when combined with an "_id" should conventionally match the columns.
162- $ relation = $ caller ['function ' ];
163-
164178 if (is_null ($ foreignKey ))
165179 {
166180 $ foreignKey = snake_case ($ relation ).'_id ' ;
167181 }
168182
183+ $ instance = new $ related ;
184+
169185 // Once we have the foreign key names, we'll just create a new Eloquent query
170186 // for the related models and returns the relationship instance which will
171187 // actually be responsible for retrieving and hydrating every relations.
172- $ instance = new $ related ;
173-
174188 $ query = $ instance ->newQuery ();
175189
176- return new BelongsTo ($ query , $ this , $ foreignKey , $ relation );
190+ $ otherKey = $ otherKey ?: $ instance ->getKeyName ();
191+
192+ return new BelongsTo ($ query , $ this , $ foreignKey , $ otherKey , $ relation );
177193 }
178194
179195 /**
0 commit comments