Laravel 5.1 Create or Update on Duplicate

前端 未结 6 944
悲哀的现实
悲哀的现实 2020-12-03 01:29

In Laravel 5.1, for MySQL insert, I want to see if the record already exists and update on duplicate or create new if none exists.

I have already searched SO where t

6条回答
  •  無奈伤痛
    2020-12-03 01:39

    In my case this was caused by a simple mismatch on the auto incrementing key in my model table.

    Getting the current max id:

    SELECT max(id) from companies
    

    Then setting the sequence value one higher:

    select setval('companies_id_seq', 164);
    

    eliminated the error.

提交回复
热议问题