drs135's avatar

Update Function crud

I'm trying to figure out how to implement an edit function. I know that its similar to the create function but instead of creating a new object you find the existing object through its id, however I cant seem to figure out how to do it.

public function edit()
    {
        //
        return view('modules.update');
    }

public function update(Request $request, $id)
    {
        //
        $module = Module::find($id);
        $module->course = $request->course;
 	$module->save();
        return redirect('/modules/all')->with('created', 'post Created');
    }
<form method="post" action="/modules/edit/2" enctype="multipart/form-data">
@method('PUT')
@csrf
<v-text-field name="course" id="course"></v-text-field>
<v-text-field name="school" id="school"></v-text-field>
<v-btn type="submit">submit</v-btn>
</form>

Here is the controller + the blade file

0 likes
2 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

You are trying to save to the edit route (PUT). The edit route is normally a get route where you have a form you can update

public function edit(Post $post)
    {
        
        return view('modules.edit', $post);
    }

//assuming this is the edit view
<form method="post" action="/modules/update/{{$post->id}}" enctype="multipart/form-data"> //or whatever your update url is
drs135's avatar

I've got the form to work, however instead of updating it fully replaces every value. Is there any way to fix this without having to use {{old($data->name)}}

This is the form

<form method="post" action="/modules/edit/{{$module->id}}" enctype="multipart/form-data">

@csrf
<h1>{{$module->id}}</h1>
<v-text-field name="course" id="course"></v-text-field>
<v-text-field name="school" id="school"></v-text-field>
<v-btn type="submit">submit</v-btn>
</form>

I've read that u need to use the method as PUT or PATCH, but whenever I use these two methods the code breaks

Please or to participate in this conversation.