我抓取網站https://news.ycombinator.com/news?p=1的資料
某些標題包含逗號,例如:Lorinda Cherry,dc, bc, eqn 的作者已經死亡
所以,當我將模型(作為 Laravel CLI 命令的一部分)保存到 DB 中時,會出現錯誤。代碼:
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());
}
錯誤:
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))
顯然,dc, bc, eqn 的作者 Lorinda Cherry has dead的值在方法中沒有被參考save()為 Laravel 模型項,因此它的內容與另一個查詢引數混合在一起......
為什么在 Laravel 中?
怎么修 ?
uj5u.com熱心網友回復:
您正在嘗試將字串保存在列是整數型別的點列中謝謝
uj5u.com熱心網友回復:
嘗試這個:
$news->title = '\'' . $item['title'] . '\'';
uj5u.com熱心網友回復:
您在Incorrect integer value中的問題
因此,您可以轉換點值:
$news->points = (int) $item['points'];
uj5u.com熱心網友回復:
或者,您可以使用驗證來了解您的錯誤,并且在您的代碼中 request['points'] is not integer 您嘗試在資料庫中保存非整數的內容您應該將資料庫上的資料型別更改為字串或您應該將整數資料提交到您的端點。 在將某些內容保存到資料庫之前,您應該驗證我強烈推薦的資料
$validatedData = $request->validate([
'link'=>'required|url',
'title' => 'required|string|max:255',
'date' => 'required|date',
'points'=>'required|numeric'
]);
News::create($validatedData);
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/426923.html
