0

I am experiencing some weird issue with updating records in Laravel. I use this code to update a record:

$student = new Student();
$student->exists = true;
$student->reg_number = $request->post('reg_number');
$student->name = $request->post('name');
$student->level_id = $request->post('level_id');
$student->status = $request->post('status'); 
$student->save();

The code executes fine for the first time. However, when I try to update the same record again, I get the error

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'F17/2019/2019' for key 'PRIMARY' (SQL: insert into students (reg_number, name, level_id, updated_at, created_at) values (F17/2019/2019, John Does, 5, 2019-08-08 10:31:52, 2019-08-08 10:31:52)).

However, after sometime, say half an hour or so, I can update the record again without the error, but only once. A second try brings back the error.

I have tried various other techniques discussed in this question, but the results are identical. The error suggests that Laravel is opting to use INSERT instead of UPDATE SQL statement, whereas the record exists and I have explicitly specified that it should update an existing record.

Does anyone know what's going on here?


I am usng MySQL, my students table PRIMARY KEY is reg_number of type VARCHAR. The student controller looks like so:

namespace App;
use Illuminate\Database\Eloquent\Model; 
class Student extends Model
{
    protected $fillable = [
        'reg_number', 
        'name', 
        'level_id', 
        'status' 
    ];
    protected $primaryKey = 'reg_number';
    public $incrementing = false;
}
handlerFive
  • 870
  • 10
  • 23
Supreme Dolphin
  • 2,248
  • 1
  • 14
  • 23
  • Please dont use new Student(); for existing records, use Student::find() – dparoli Aug 08 '19 at 10:55
  • When you set ```exists```, the code updates an existing record. Please follow the link in my question. Also, the fact that the record updates for the first time should hint you that the error isn't arising from using ```new Student()```. – Supreme Dolphin Aug 08 '19 at 11:04
  • The link has answers of more then 3 years ago, I would not trust so much the exists property ther are others like `$wasRecentlyCreated` if you dig the source. The recommended way to get existing models is to hydrate them from the DB. – dparoli Aug 08 '19 at 11:11
  • @dparoli The ```exists``` property actually works in Laravel 5.8. Try it out. – Supreme Dolphin Aug 08 '19 at 11:13
  • In your case it doesn't work, your question is for this. I don't understand why you don't want to find the model in the DB, am I missing something? – dparoli Aug 08 '19 at 11:18
  • @dparoli Check out my answer below and various other comments I have made. It actually does work. – Supreme Dolphin Aug 08 '19 at 11:20

3 Answers3

3

You have to find the student you want to update. Because New will always create new instance of model and when you save, it will create new record instead of update.

$student = Student::find($id);
$student->exists = true;
$student->reg_number = $request->post('reg_number');
$student->name = $request->post('name');
$student->level_id = $request->post('level_id');
$student->status = $request->post('status'); 
$student->save();

Another way to update record is:

$data = [
   // Column values you want to update
   'exists' => 'value' 
    ... 
    'status' => 'value'
];

Student::where('id', $id)->update($data)
Sagar Gautam
  • 9,049
  • 6
  • 53
  • 84
  • None of these work. Please check the link in my question. It has all these solutions. However, I have discovered that the error is arising from some other part of my code that I have no posted. I will close the question as the problem is not actually with the code I posted. PS: My code would still update the record without modifications. This is because when you set ```exists``` to a record, it uses the update function, and does not create a new one. – Supreme Dolphin Aug 08 '19 at 11:01
  • @TroubleZero You can use updateOrCreate method to acheive that. – Sagar Gautam Aug 08 '19 at 11:06
  • Actually, I was using some sort of ```updateOrCreate```. The function I was using to call the code to update the record is something like this: ```public function addStudent(Request $request) { if($request->post('status') !== null){ $this->updateStudent($request); return false; }```... and this is where the error was arising from as I was checking ```$request->post('status')``` which was sometimes null when updating. – Supreme Dolphin Aug 08 '19 at 11:09
0

try to change value from false to true

public $incrementing = true;
  • I have already discovered what I was doing wrong. However, ```$incrementing``` is needed as the primary key is of VARCHAR data type. – Supreme Dolphin Aug 08 '19 at 11:17
0

I had an error in some other part of the code that I hadn't posted. I have the function

public function addStudent(Request $request) {
    if($request->post('status') !== null){
        $this->updateStudent($request);
        return false;
    }
...

and this is where the error was arising from as I was checking $request->post('status') which was sometimes null when updating. Please vote the question to be closed as the error is not arising from the part of the code I have posted.

I was banging my head because Laravel is sometimes ambiguous with error reporting. If it stated the line number or the function that was causing the error, I would have debugged it much easily and wouldn't have ended up posting an irrelevant question.

Supreme Dolphin
  • 2,248
  • 1
  • 14
  • 23