簡體   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