PHP Laravel save() is not updating record

I am a beginner in laravel. I was updating records but I can’t figure out what is wrong with my $student->save();
My controller code is as follows


namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppModelsStudent;
use PHPUnitFrameworkMockObjectBuilderStub;

class HomeController extends Controller
    public function read() {
        $students = Student::all();
        return view('read',['Students'=>$students]);

    public function insert() {
        return view('insert');

    public function insertPost(Request $req) {
        $student=new Student();
        $student->Name = $req->input('name');
        $student->Marks = $req->input('marks');
        return redirect('/');

    public function update($id) {
        $student = Student::find($id);
        return view('update',['Student'=>$student]);

    public function delete($id) {
        $student = Student::find($id);

    public function updatePost(Request $req) {
        $student = Student::find($req->input('id'));

        // Student::where('ID',$req->input('id'))
        //         ->update(['Name'=>$req->input('name'),
        //                   'Marks'=>$req->input('marks')]);
        return redirect('/');

The Main main in updatePost(); is causing the records not updating

$student = Student::find($req->input('id'));

I changed the way of updating records to


and it worked. But I wanted to know at which part I was making mistake in it.


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You need to set your primary key to ‘ID’
By default the primary key or PK name is ‘id’
But in your code it changed to ‘ID’
So you need to go to your User.php model class
and add this line

    public $primaryKey = 'ID';

Method 2

One thing you can try is using Laravel’s update method. update will handle the save internally.

$student = Student::findOrFail($req->input('id')));

    'Name' => $req->input('name'),
    'Marks' => $req->input('marks'),

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x