0

https://github.com/Maatwebsite/Laravel-Excelこのパッケージを使用しています。そして、ファイルをアップロードすると、 dd(result) は

    CellCollection {#842 ▼

         #title: null
        #items: array:4 [▼
         "news_title" => "7th AGM of ADBL today; endorsing 47% cash"            
         "desc" => "The AGM will be endorsing 47% percent cash dividend to its shareholders from the net profit it earned in last fiscal year 2070/71. "
            "link" => "http://www.sharesansar.com/viewnews.php?id=26224&cat=news"
         "stock_code" => "LBL"
        ]
    }

したがって、ここでは #items にデータが含まれていますが、 #title が出力される理由はわかりません。データを保存しようとすると、#title が原因で Integrity Violation エラーが発生します。それで、解決策はありますか?

ここにデータを保存するための私のコードがあります

     public function excelNews()
    {
        if (Input::hasFile('file')) {
            $file = Input::file('file');
            Excel::load($file, function($reader) {
                $reader->setDateFormat('j/n/Y H:i:s');
                $results = $reader->get();
                 foreach ($results as $result)
                {
                    dd($result); // for testing
                    $news = new StockNews;
                    $news->title = $result->news_title;
                    $news->desc = $result->desc;
                    $news->save()
                }

         });
    }
        Flash::success('News has been successfully updated');
        return redirect::back();
    }

エラーメッセージ

整合性制約違反列 'title' は null にできません

4

1 に答える 1

1

タイトルがnullでDBに保存しようとしたため、エラーが発生しました。

問題を解決するには2つの方法があります

移行では、列を null 許容に設定します

$table->string('title')->nullable();

ソース: http://laravel.com/docs/4.2/schema#adding-columns

または値を確認し、null の場合はタイトルを空の文字列に設定します

$news->title = ($result->news_title) ? $result->news_title : '' ;
于 2015-04-10T06:36:12.557 に答える