Despotars
Despotars

Reputation: 571

Insert Ignore Laravel 4

I want to do an insert in the database, but I want to make an insert ignore, to ignore if there is already a value in the primary key. How do I do this with eloquent? this is my code in eloquent:

return LogMonth::insert(array('users_id'  => $user,
                         'log'       => $Log,
                         'month'     => $Month,
                         'year'      => $Mear,));

Upvotes: 4

Views: 3593

Answers (1)

Joseph Silber
Joseph Silber

Reputation: 220026

Use the firstOrCreate method:

return Log::firstOrCreate([
    'users_id'  => $user,
    'log'       => $Log,
    'month'     => $Month,
    'year'      => $Year,
]);

Upvotes: 6

Related Questions