Strange insert error..Integrity constraint violation

Posted 9 months ago by Webiondev123

Ok, whenever I am trying to insert column 'to_' using eloquent it gives me error integrity constraint violation but when I insert using DB::table it inserts..why?

QLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`databasename`.`message`, CONSTRAINT `message_to__foreign` FOREIGN KEY (`to_`) REFERENCES `users` (`id`)) (SQL: insert into `message` (`from`, `updated_at`, `created_at`) values (2, 2018-02-21 07:29:05, 2018-02-21 07:29:05))

function insertThis($id){
  DB::table('message')->insert(array('from' => auth()->user()->id,'to_'=>$id, 'message' => 'sdvsdvsdv', 'date'=>date('Y-m-d'))); //ok






namespace App;

use Illuminate\Database\Eloquent\Model;
class Message extends Model

    protected $table='message';
    protected $fillable = [
    'from', 'to_', 'message','date' 



Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up


Reply to

Use Markdown with GitHub-flavored code blocks.