Niamh Flannery
Niamh Flannery

Reputation: 91

Having trouble getting update in Laravel to work

I am currently working on a project for University. I'm trying to get an update function to work. I have shown my code below any help or direction would be greatly appreciated.

I have the view file showing whats currently in the database, I want to edit one field and update this back to the database.

Thanks in advance.

Controller

        <?php

    namespace App\Http\Controllers;

    use Illuminate\Http\Request;


    use Auth;


    class LessonsController extends Controller


    {

    public function update(Request $request) 

        {
            $lesson->level = $request->level;
            $lesson->update();

            return redirect('./lessons/');
        }

    }

view file

<html>

<head>
    <title></title>

</head>

<body>


    <form  class="form-horizontal" method="post" action="./{{ $lesson->id }}"> 
                    {{ method_field('put') }}
                    {{ csrf_field() }}
                    <input type="hidden" name="_method" value="PUT" />
                    <div class="form-group">
                        <label class="col-md-4 control-label">Username</label>
                        <div class="col-md-6">
                            <input type="text" name="level" class="form-control" value="{{ $lesson->level }}">
                        </div>
                    </div>


                    <div class="form-group">
                    <div class="col-md-6 col-md-offset-4">
                        <input type="submit" id="update-account" class="btn btn-primary" value="Update">
                    </div>
                    </div>

                </form>



</body>

Upvotes: 0

Views: 59

Answers (3)

Jems
Jems

Reputation: 1706

You need ID(primary key) to update the data

public function update(Request $request) 
    {
        $lesson = Lesson::find($id);
        $lesson->level = $request->level;
        $lesson->save();

        return redirect('./lessons/');
    }

I think if you only want to update the level.. better to use PATCH method..

I think that put method is using when you want to update all table data, but patch method is using to update not all parameter of your model #CMIIW

Upvotes: 1

Brian Lee
Brian Lee

Reputation: 18187

Include the Lesson model namespace at the top of your file:

use App\Lesson;
// or App\Models\Lesson if you placed it under a different directory, for example.

You can simplify the query to find the Lesson record by using route model binding:

public function update(Request $request, Lesson $lesson) 
{
    $lesson->update([
        'level' => $request->level
    ]);

    return redirect()->route('lessons.show', compact('lesson'));
}

Update the form action, as well:

action="{{ route('lessons.update', $lesson->id) }}"

Upvotes: 1

Arash Hatami
Arash Hatami

Reputation: 5541

  1. You need a hidden input field for keep lesson id

<input name="id" type="hidden" value="{{ $lesson->id }}" />

  1. There is many ways to get request fields in controller. You can use $request->get('key')

And then :

$lesson = Lesson::where('id', $request->get('id'))->firstOrFail();
$lesson->level = $request->get('level');
$lesson->save();

Upvotes: 1

Related Questions