0

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

5
  • Did you set column title to be unique in DB? Commented Apr 10, 2015 at 6:06
  • No, none of the fields are unique. And even the data are stored in the db. But it throws error too. Commented Apr 10, 2015 at 6:20
  • What is the error message? Commented Apr 10, 2015 at 6:22
  • It says that Integrity Constraint Violation column 'title' can not be null Commented Apr 10, 2015 at 6:27
  • When I try like if(!empty($result->title)){ store the data} else { } It works fine Commented Apr 10, 2015 at 6:28

1 Answer 1

1

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 : '' ;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.