Laravel-模型创建后BelongsTo关系中的意外行为


Laravel - Unexpected behavior in BelongsTo relationship after model creation

在创建模型后,我在BelongsTo关系中有一个奇怪的行为。

我正在创建地址并将一个国家与之关联,为了实现这一点,我使用地址模型的创建方法,这是代码片段:

$country = Country::find($input['country']);
$user = User::find($input['user']);
$address = Address::create($input);       // Address created and saved
$address->user()->associate($user);       // Works perfectly
$address->country()->associate($country); // Does not work :(
$address->push();

在那之后,我在国家地址关联完成的行中出现了这个错误:

ErrorException in BelongsTo.php line 75:
Undefined property: App'Models'Address::$country

奇怪的行为是,地址-用户关联正在工作,只有在创建地址后,我使用find方法检索地址,地址和国家之间的关联才有可能。

$country = Country::find($input['country']);
$user = User::find($input['user']);
$address = Address::create($input);
$address = Address::find($address->id);   // Why?
$address->user()->associate($user);       // Always works
$address->country()->associate($country); // Now is working :)
$address->push();

你能解释一下协会为什么会这样做吗?如果有可能的话?

然后,我比较了create方法和find方法返回的实例,注意到create方法的实例中丢失了一些属性:

$createAddress = Address::create($input);
$findAddress = Address::find($address->id);
dd(['create' => $createAddress, 'find' => $findAddress]);

这是dd()的输出:

array:2 [▼
  "create" => Address {#211 ▼
    #table: "addresses"
    #guarded: array:1 [▶]
    #fillable: array:9 [▶]
    #hidden: array:3 [▶]
    #dates: array:3 [▶]
    #connection: null
    #primaryKey: "id"
    #perPage: 15
    +incrementing: true
    +timestamps: true
    #attributes: array:12 [▼
      "alias" => "Home"
      "street" => "Home St."
      "number_external" => "123"
      "number_internal" => "456"
      "neighborhood" => "My beautiful neighborhood"
      "city" => "Cool City"
      "town" => "Cool Town"
      "state" => "Cool State"
      "zip_code" => "123456"
      "updated_at" => "2015-12-09 11:10:14"
      "created_at" => "2015-12-09 11:10:14"
      "id" => 43
    ]
    #original: array:12 [▼
      "alias" => "Home"
      "street" => "Home St."
      "number_external" => "123"
      "number_internal" => "456"
      "neighborhood" => "My beautiful neighborhood"
      "city" => "Cool City"
      "town" => "Cool Town"
      "state" => "Cool State"
      "zip_code" => "123456"
      "updated_at" => "2015-12-09 11:10:14"
      "created_at" => "2015-12-09 11:10:14"
      "id" => 43
    ]
    #relations: []
    #visible: []
    #appends: []
    #dateFormat: null
    #casts: []
    #touches: []
    #observables: []
    #with: []
    #morphClass: null
    +exists: true
    +wasRecentlyCreated: true
    #forceDeleting: false
  }
  "find" => Address {#232 ▼
    #table: "addresses"
    #guarded: array:1 [▶]
    #fillable: array:9 [▶]
    #hidden: array:3 [▶]
    #dates: array:3 [▶]
    #connection: null
    #primaryKey: "id"
    #perPage: 15
    +incrementing: true
    +timestamps: true
    #attributes: array:16 [▼
      "id" => "43"
      "alias" => "Home"
      "street" => "Home St."
      "number_external" => "123"
      "number_internal" => "456"
      "neighborhood" => "My beautiful neighborhood"
      "city" => "Cool City"
      "town" => "Cool Town"
      "state" => "Cool State"
      "country" => "0"
      "zip_code" => "123456"
      "is_legal_address" => "0"
      "user_id" => "0"
      "deleted_at" => null
      "created_at" => "2015-12-09 11:10:14"
      "updated_at" => "2015-12-09 11:10:14"
    ]
    #original: array:16 [▼
      "id" => "43"
      "alias" => "Home"
      "street" => "Home St."
      "number_external" => "123"
      "number_internal" => "456"
      "neighborhood" => "My beautiful neighborhood"
      "city" => "Cool City"
      "town" => "Cool Town"
      "state" => "Cool State"
      "country" => "0"
      "zip_code" => "123456"
      "is_legal_address" => "0"
      "user_id" => "0"
      "deleted_at" => null
      "created_at" => "2015-12-09 11:10:14"
      "updated_at" => "2015-12-09 11:10:14"
    ]
    #relations: []
    #visible: []
    #appends: []
    #dateFormat: null
    #casts: []
    #touches: []
    #observables: []
    #with: []
    #morphClass: null
    +exists: true
    +wasRecentlyCreated: false
    #forceDeleting: false
  }
]

模型:

class Address extends Model {
  public function user() {
    return $this->belongsTo('App'Models'User', 'user_id');
  }
  public function country() {
    return $this->belongsTo('App'Models'Country', 'country');
  }
}
class Country extends Model {
  public function addresses() {
    return $this->hasMany('App'Models'Address', 'country');
  }
}
class User extends Model {
  public function addresses() {
    return $this->hasMany('App'Models'Address', 'user_id');
  }
}

提前感谢您的帮助。

这里的问题是您使用的列与您的关系相同。在addresses表中有列country,并且创建了名称为country的关系。

您应该更改关系名称,或者将country列名更改为例如country_id(如果可能的话,第二个会更好)