user3127109
user3127109

Reputation: 3763

Error using https://github.com/Maatwebsite/Laravel-Excel

I am using https://github.com/Maatwebsite/Laravel-Excel this package. And when I upload my file the and dd(result) is

    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"
        ]
    }

So, here the #items contains my data whereas I don't know why is #title being output. And when I try to store my data, I am getting Integrity Violation Error due to this #title? So, is there a solution?

Here is my code to store the data

     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();
    }

Error Message

Integrity Constraint Violation column 'title' can not be null

Upvotes: 0

Views: 985

Answers (1)

mininoz
mininoz

Reputation: 5958

The error happends because the title is null and try to save it to DB.

There are 2 ways to solve the problem

In migration, Set the column to be nullable

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

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

or check the value and if it null set the title to be an empty string

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

Upvotes: 1

Related Questions