Die eloquente morphOne-Beziehung ist nicht auf eine Beziehung beschränkt

Ich habe ein Problem mit einem EloquentenmorphOne Beziehung, in der neue Einträge erstellt werden, anstatt die bereits vorhandenen zu aktualisieren.

Grundsätzlich habe ich eine Reihe von Modellen (zum Beispiel lassen Sie uns sagenPerson undBuilding), dass beide einen Ort benötigen, also habe ich einen erstelltLocation Modell:

class Location extends Eloquent {

    public function locationable()
    {
        return $this->morphTo();
    }

}

Dann habe ich in meinen anderen Modellen Folgendes:

class Person extends Eloquent {

    // ...

    /**
     * Get the person's location
     * 
     * @return Location
     */
    public function location()
    {
        return $this->morphOne('Location', 'locationable');
    }

    // ...
class Building extends Eloquent {

    // ...

    /**
     * Get the building's location
     * 
     * @return Location
     */
    public function location()
    {
        return $this->morphOne('Location', 'locationable');
    }

    // ...

Wenn ich den folgenden Testcode ausführe, wird der Standorteintrag in Ordnung erstellt. Wenn ich ihn wiederhole, werden jedoch weitere Einträge erstellt.

$person = Person::first();

$loc = new Location;

$loc->lat = "123";
$loc->lng = "321";

$person->location()->save($loc);

Mache ich hier etwas falsch? Hätte ich erwartetmorphOne Um dies auf einen Eintrag pro Typ zu beschränken, sollte der letzte Eintrag in der folgenden Tabelle nicht vorhanden sein:

+---------------------+--------------------------+
|  locationable_id    |   locationable_type      |
+---------------------+--------------------------+
|  2                  |  Building                |
|  3                  |  Building                |
|  2                  |  Person                  |
|  2                  |  Building                |
+---------------------+--------------------------+

Antworten auf die Frage(3)

Ihre Antwort auf die Frage