错误使用https://github.com/Maatwebsite/Laravel-Excel

我正在使用https://github.com/Maatwebsite/Laravel-Excel这个包。 而当我上传我的文件和dd(结果)是

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,我正在获得完整性违规错误? 那么,有没有解决scheme?

这是我的代码来存储数据

  public function excelNews() { if (Input::hasFile('file')) { $file = Input::file('file'); Excel::load($file, function($reader) { $reader->setDateFormat('j/n/YH: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(); } 

错误信息

完整性约束违例列'标题'不能为空

错误发生的原因是标题为空,并尝试将其保存到数据库。

有两种方法可以解决这个问题

在迁移中,将列设置为可空

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

来源: http : //laravel.com/docs/4.2/schema#adding-columns

或检查值,如果它为null设置标题为空string

 $news->title = ($result->news_title) ? $result->news_title : '' ;