繁体   English   中英

laravel 5.7如何解决“无效的日期时间格式:1366不正确的整数值:”错误?

[英]How to fix “Invalid datetime format: 1366 Incorrect integer value: ” error in laravel 5.7?

我试图在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()函数定义了一对一的逆关系,如下所示

     //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不正确的整数值:'[{“ id”:1,“ name”:“ Example Name”,“ email”:“ example@email.com第1行的“ account_id”列为“'(SQL:更新invoices设置account_id = [{“ id”:1,“名称”:“示例名称”,“电子邮件”:“ example@email.com”,“ created_at” :“ 2019-01-02 15:37:41”,“ updated_at”:“ 2019-01-02 15:37:41”}], updated_at = 2019-01-02 16:05:13其中id = 17)

请帮助我如何解决此错误。

您必须删除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');
 }

关联所有关系后,还请保存以避免多个数据库事务。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM