私はlaravelには新人ですが、私は本当にそれが本当に大好きです。 は今、私は雄弁で少しプレーし、質問が来た:Laravel hasOne保存方法ID
私は2つのアドレス(BillingAdressとDeliveryAddress)
Cusomers-フィールドでCusomersを持っている:ID、ファーストネーム、姓、BillingAddress、DeliveryAddress アドレス - フィールド:ID、住所、郵便番号、都市、国
私は顧客を作成したいと思います。作成した後、彼に住所を割り当てたいと思います。
カスタマーモデル:
class Customer extends Model
{
public function deliveryAddress() {
return $this->hasOne("App\Address");
}
public function billingAddress() {
return $this->hasOne("App\Address");
}
}
アドレスモデル:
class Address extends Model
{
public function customer() {
return $this->belongsTo("App\Customer");
}
}
コントローラー:
$c = new Customer;
$c->Firstname = "John";
$c->Lastname = "Doe";
$c->save();
$billingAddress = new Address;
$billingAddress->address = "Exampleroad 3";
$billingAddress->zip = 11111;
$billingAddress->city = "Examplecity";
$deliveryAddress = new Address;
$deliveryAddress->address = "Doestreed 21";
$deliveryAddress->zip = "44444";
$deliveryAddress->city = "Doedown";
$c->billingAddress()->save($billingAddress);
$c->deliveryAddress()->save($deliveryAddress);
しかしRIGH今では、アドレステーブルのcusomer_idは保存されますが、cusomerテーブルのdeliveryAddressとbillingAddressはnullです。
私はこれをしようと試みたが、私はHTTP 500エラー –
がOK得る今私は、機能が正確に列のような名前のため、私はだったと思います??今私はfunctioの名前を変更しましたが、フィールドはまだデータベースではnullです –