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

时间:2015-12-09 17:19:17

标签: php laravel laravel-5 eloquent laravel-5.1

在模型创建后,我在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');
  }
}

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

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

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