Home > database >  Laravel: model instance value is not quoted at method `save()`
Laravel: model instance value is not quoted at method `save()`

Time:02-17

I scrape data of the site https://news.ycombinator.com/news?p=1

Some title contain commas, eg.: Lorinda Cherry, author of dc, bc, eqn has died
So, when I make to save the model (as part of the Laravel CLI command) into DB there is an error. The code:

  try {
            $news = new News; 
            $news->link =   $item['link']  ; 
            $news->title = $item['title'] ;
            $news->date = explode('T',  $item['date'] )[0];
            $news->points = $item['points'];         
            $news->save();
      } catch (Exception $e ){
                $this->error($e->getMessage());
      }

Error:

SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '
    763 ' for column 'points' at row 1 (SQL: insert into `news` (`link`, `title`, `date`, 
    `points`, `updated_at`, `created_at`) values (https://ncwit.org/profile/lorindacherry/, 
    Lorinda Cherry, author of dc, bc, eqn has died, 2022-02-15,
    763 , 2022-02-16 11:37:39, 2022-02-16 11:37:39))

Obviously the value Lorinda Cherry, author of dc, bc, eqn has died is not quoted at method save() as the Laravel model item and therefore its content gets mixed with another query parameter...

Why is that in Laravel ?
How to fix ?

CodePudding user response:

you are trying to save string in points column where column is integer type Thanks

CodePudding user response:

Try this:

$news->title = '\'' . $item['title'] . '\'';
  • Related