スポンサーリンク

【Laravel】insertしようとすると型エラーが出たときの対応

以下のようなinsert文をかいてみた

DB::table(‘comment_yahoo’)->insert
([
‘feed_id’ => $feed_id,
‘user_name’ => $val[‘name’],
‘good’ => $val[‘good’],
‘bad’ => $val[‘bad’],
‘comment’ => $val[‘comment’],
‘image_path’ => “1”,
‘created_at’ => $created_at,
‘updated_at’ => $created_at
]);

すると以下のようなエラー

The Response content must be a string or object implementing __toString(),”boolean” given.

 

stringじゃなくてbooleanが与えられてるって書かれてる

よくわからん

型の問題かと思ってDBで定義しているテーブルの型と入れようとしている数値の型を調べてみると

good,badはint型でテーブル定義されている

入れようとしていた値はstring型

以上のように型が違っていた

なので以下のようにintにキャストして試してみるとうまく行った

DB::table(‘comment_yahoo’)->insert
([
‘feed_id’ => $feed_id,
‘user_name’ => $val[‘name’],
‘good’ => (int)$val[‘good’],
‘bad’ => (int)$val[‘bad’],
‘comment’ => $val[‘comment’],
‘image_path’ => “1”,
‘created_at’ => $created_at,
‘updated_at’ => $created_at
]);

 

 

コメント

タイトルとURLをコピーしました