我试图在laravel 5.7中创建具有多个一对一关系的记录。以下是我的操作方式,
迁移
//in create_invoices_table.php
public function up()
{
Schema::create('invoices', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('service_id')->unsigned()->nullable();
$table->integer('account_id')->unsigned()->nullable();
$table->timestamps();
// foreign keys
$table->foreign('service_id')->references('id')->on('services')->onDelete('cascade');
$table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade');
});
}
// in create_accounts_table.php
public function up()
{
Schema::create('accounts', function (Blueprint $table){
$table->increments('id');
$table->string('name');
$table->string('email');
$table->timestamps()
)};
}
// in the create_services_table.php
public function up()
{
Schema::create('services', function (Blueprint $table){
$table->increments('id');
$table->string('name');
$table->float('price', 8, 2);
$table->timestamps();
)};
}
模型
Account
模型具有一个hasOne()
函数,该函数定义了与发票的一对一关系。即
//in Account.php
public function invoice() {
return $this->hasOne('App\Invoice');
}
Service
模型具有hasOne()
函数,该函数还定义了与发票的一对一关系。即
//in Service.php
public function invoice() {
return $this->hasOne('App\Invoice');
}
使用invoice model
函数在belongsTo()
中定义一对一的逆关系a如下
//in Invoice.php
public function account() {
return $this->belongsTo('App\Account');
}
public function service() {
return $this->belongsTo('App\Service');
}
InvoiceController.php
现在,我正尝试创建一个发票记录,该记录将与以下的account
模型和service
模型相关联
public function store(Request $request)
{
$account = Account::find([$request->get('aid')]);
$service = Service::find([$request->get('sid')]);
$invoice = new Invoice();
$invoice->name = self::getAccountName($request->get('aid'));
// save the invoice to database
$invoice->save();
// add service and account to the invoice
$invoice->account()->associate($account)->save();
$invoice->service()->associate($service)->save();
return redirect()->route('invoices.index');
}
这给了我我无法修复的以下错误。
Illuminate \ Database \ QueryException(22007)SQLSTATE [22007]: 无效的日期时间格式:1366不正确的整数值: 第1行“ account_id”列的'[{{id“:1,” name“:”示例名称“,” email“:” example@email.com“'(SQL:更新
invoices
设置{{ 1}} = [{{“ id”:1,“ name”:“ Example Name”,“ email”:“ example@email.com”,“ created_at”:“” 2019-01-02 15:37:41“, “ updated_at”:“ 2019-01-02 15:37:41”}],account_id
= 2019-01-02 16:05:13其中updated_at
= 17)
请帮我解决该错误。
答案 0 :(得分:0)
您必须删除find中的方括号。如果您将数组作为参数传递,那么雄辩的查找将返回一个集合。
public function store(Request $request)
{
$account = Account::find($request->get('aid')); // remove square brackets around $request
$service = Service::find($request->get('sid')); // remove square brackets around $request
$invoice = new Invoice();
$invoice->name = self::getAccountName($request->get('aid'));
// add service and account to the invoice
$invoice->account()->associate($account); // remove save()
$invoice->service()->associate($service); // remove save()
// save the invoice to database
$invoice->save();
return redirect()->route('invoices.index');
}
在关联所有关系后也要保存以避免多个数据库事务。