@@ -114,66 +114,82 @@ public function getTable()
114
114
}
115
115
116
116
/**
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 )
124
125
{
125
126
$ foreignKey = $ foreignKey ?: $ this ->getForeignKey ();
126
127
127
128
$ instance = new $ related ;
128
129
129
- return new HasOne ($ instance ->newQuery (), $ this , $ foreignKey );
130
+ $ localKey = $ localKey ?: $ this ->getKeyName ();
131
+
132
+ return new HasOne ($ instance ->newQuery (), $ this , $ foreignKey , $ localKey );
130
133
}
131
134
132
135
/**
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 )
140
144
{
141
145
$ foreignKey = $ foreignKey ?: $ this ->getForeignKey ();
142
146
143
147
$ instance = new $ related ;
144
148
145
- return new HasMany ($ instance ->newQuery (), $ this , $ foreignKey );
149
+ $ localKey = $ localKey ?: $ this ->getKeyName ();
150
+
151
+ return new HasMany ($ instance ->newQuery (), $ this , $ foreignKey , $ localKey );
146
152
}
147
153
148
154
/**
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 )
156
164
{
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
+ }
158
174
159
175
// If no foreign key was supplied, we can use a backtrace to guess the proper
160
176
// foreign key name by using the name of the relationship function, which
161
177
// when combined with an "_id" should conventionally match the columns.
162
- $ relation = $ caller ['function ' ];
163
-
164
178
if (is_null ($ foreignKey ))
165
179
{
166
180
$ foreignKey = snake_case ($ relation ).'_id ' ;
167
181
}
168
182
183
+ $ instance = new $ related ;
184
+
169
185
// Once we have the foreign key names, we'll just create a new Eloquent query
170
186
// for the related models and returns the relationship instance which will
171
187
// actually be responsible for retrieving and hydrating every relations.
172
- $ instance = new $ related ;
173
-
174
188
$ query = $ instance ->newQuery ();
175
189
176
- return new BelongsTo ($ query , $ this , $ foreignKey , $ relation );
190
+ $ otherKey = $ otherKey ?: $ instance ->getKeyName ();
191
+
192
+ return new BelongsTo ($ query , $ this , $ foreignKey , $ otherKey , $ relation );
177
193
}
178
194
179
195
/**
0 commit comments