laravel/framework — pull request #2487 by m4tthumphrey (2013-10-15 14:44:32+00:00)
Title: Option to set a different field, other than the primary key when defining BelongsTo and HasOneOrMany relations

Eloquent currently assumes that you would want to always join onto the primary key field of the relation table. This may not always be the case (in my case we are using a legacy database design where some tables do not even have a "normal" primary key).

This PR allows the developer to define a `hasMany`, `hasOne` or `belongsTo` relationship using a specific field to join to as a 3rd parameter on the corresponding methods.

Usage:

app/models/Book.php

```php
class Book extends Eloquent
{
// ...

public function author()
{
return $this->belongsTo('Author', 'author_code', 'code');
}
}
```

app/models/Author.php

```php
class Author extends Eloquent
{
protected $primaryKey = 'id';

// ...

public function books()
{
return $this->hasMany('Books', 'author_code', 'code');
}
}
```