0

I am following a tutorial that shows how to connect to MySql using laravel, but while trying to update the values I ran into an error, I did try to find something similar but failed at that. The answers I found said that the object must be empty, but if I'm updating an existing values it's not empty. A tutorial I am following https://www.webslesson.info/2018/01/insert-update-delete-in-mysql-table-laravel-tutorial.html

So I have a form that displays the data from database in a file index.blade.php with a edit, a form that displays specific user data in a fine edit.blade.php and the methods in StudentController.php file.

They look like this:

Table of the user data and the edit button index.blade.php

       <table class="table table-bordered">
            <tr>
                <th>First name</th>
                <th>Last name</th>
                <th>Edit</th>
                <th>Delete</th>
            </tr>
            @foreach($students as $row)
                <tr>
                    <th>{{$row['name']}}</th>
                    <th>{{$row['surname']}}</th>
                    <th><a href="{{action('StudentController@edit', $row['id'])}}" class="btn btn-primary">Edit</a></th>
                    <th></th>
                </tr>
            @endforeach
        </table>

Form in the edit.blade.php file

      <form action="{{action('StudentController@update','$id')}}" method="post">
            {{csrf_field()}}
            <input type="hidden" name="_method" value="PATCH">
            <div class="form-group">
                <input type="text" name="name" class="form-control" value="{{$student->name}}" placeholder="Enter first name">
            </div>
            <div class="form-group">
                <input type="text" name="surname" class="form-control" value="{{$student->surname}}" placeholder="Enter last surname">
            </div>
            <div>
                <input type="submit" class="btn btn-primary" value="Edit">
            </div>

        </form>

And the methods in the Student.Controller.php file

public function update(Request $request, $id)
{
    $this->validate($request, [
        'name'=> 'required',
        'surname'=> 'required'
    ]);
    $student = Student::find($id);
    $student->name=$request->get('name');
    $student->surname=$request->get('surname');
    $student->save();
    return redirect()->route('students.index')->with('success', 'Data Updated');
}

I get the error in StudentController.php file in the line: $student->name=$request->get('name');

6
  • Check whether your getting the values in the $request by using var_dump Commented Aug 8, 2018 at 16:29
  • check the edit.blade.php html source code, '$id' should be $id Commented Aug 8, 2018 at 16:55
  • @kenken9999 edited that, but I still get the error Commented Aug 8, 2018 at 16:58
  • what happen if change $request->get() to $request->input(), or you try dd($request->get('name')) Commented Aug 8, 2018 at 17:00
  • @kenken9999i changed to to input and now it works, not sure why, but thank you I guess :D Commented Aug 8, 2018 at 17:09

1 Answer 1

3

You aren't verifying that a student was actually found. If there is no record found, find will return null.

A common practice is to use findOrFail and let Laravel throw an exception if a record with $id is not found.

$student = Student::findOrFail($id);

Or you can handle it yourself:

$student = Student::find($id);
if (!$student) {
    return response(['error' => 'No student found']);
}
Sign up to request clarification or add additional context in comments.

5 Comments

Yes, now I can see that no student has been found, but I don't really understand how. The table at the beginning shows only the saved students and the edit button passes that student id to the edit method. Am I passing the id incorrectly?
I changed $request->get() to $request->input() and now it works, but thank you for showing me that I was not verifying that a student exists. I will use that aswell.
That doesn't really have anything to do with your error. The way you are using get/input are interchangeable, they both will do the same thing.
well, then I guess changes in the edit.blade.php HTML source code, '$id' to $id, solved my problem.
Yeah, that'd definitely cause it since there would be no literal $id record in your db. Variables aren't parsed in single quotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.