laravel/framework — pull request #4249 by jarektkaczyk (2014-04-25 11:29:04+00:00)
Title: Fix associate on MorphTo relation

This fix overrides associate method and allows you to use the method on morphTo relation fixing both issues described below.

Issue 1: Currently morphTo uses associate inherited from belongsTo which is not working at all for non-existing model:

```
$tag = new Tag;
$tag->taggable()->associate(Post::first());
// Fatal error - loop because of 'null' in this code on Model.php:

// If the type value is null it is probably safe to assume we're eager loading
// the relationship. When that is the case we will pass in a dummy query as
// there are multiple types in the morph and we can't use single queries.
if (is_null($class = $this->$type))
{
return new MorphTo(
$this->newQuery(), $this, $id, null, $type, $name
);
}
```

Issue 2: Using associate on existing model doesn't update morph_type value:

```
$tag = new Tag(['body'=>'whatever']);
Post::first()->tags()->save($tag);

$tag->taggable_id; // 1
$tag->taggable_type; // 'Post'

$tag->taggable()->associate(Category::find(5));

$tag->taggable_id; // 5
$tag->taggable_type; // 'Post' *no update here*
```