Kistlak Rajapaksha
Kistlak Rajapaksha

Reputation: 134

How To Solve This Data Update Error In Laravel

I want to Update a Registration Form data. So , at first I have created a Table for the show data. In this table , I have included an ViewForUpdate button and I have passed the Id. When I click that ViewForUpdate button , it shows unique data correctly in an another page. After I inputted new data and when I click update button , it shows this error -

MethodNotAllowedHttpException

So , How to fix this ??

Here is the RegViewUpdate.blade.php file

<html>
<head>

<body>

<form action="edit{{$users[0]->id}}" method="post" enctype="multipart/form-data">

{{ method_field('PUT') }}
{{ csrf_field() }}

<div class="form-group">
    <label>Name : *</label>
    <input type="text" class="form-control" name="name" value="{{$users[0]->name}}" required>
  </div>

  <div class="form-group">
    <label>Username : *</label>
    <input type="text" class="form-control" name="username" value="{{$users[0]->username}}" required>
  </div>

  <div class="form-group">
    <label>Password : *</label>
    <input type="password" class="form-control" name="password" value="{{$users[0]->pw}}" required>
  </div>

  <div class="form-group">
    <label>Upload Profile Picture :</label>
    <input type="file" class="form-control-file" name="file_img" aria-describedby="fileHelp">
    <small id="fileHelp" class="form-text text-muted">If U Want , U Can Skip Upload A Profile Picture</small>
  </div>

  <input type="submit" class="btn btn-primary" name="submit" value="Update">

</form>

</body>
</html>

Here is the RegViewController.php file

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;

class RegViewController extends Controller
{

    public function index()
    {
        return view('RegView');
    }

    public function show($id) {
        $users = DB::select('select * from academic where id = ?',[$id]);
        return view('RegViewUpdate',['users'=>$users]);
    }

    public function edit(Request $request, $id)
    {
        $name = $request->input('name');

        DB::update('update academic set name = ? where id = ?',[$name,$id]);
        echo "Record updated successfully.<br/>";

    }

}

Here is the Routes that I have created.

Route::get('edit/{id}','RegViewController@show');
Route::post('edit{id}','RegViewController@edit');

academic table Structure.

enter image description here

Upvotes: 0

Views: 6861

Answers (4)

Umang Patel
Umang Patel

Reputation: 133

i have found two issues with your code

  • in your view file
  • in your route

1) replace your form tag in your view file as below

{!! 
 Form::open(
               ['method' => 'PUT',
                'route' => ['update','id of data to update']
             ])
!!}

// place your form content here

{!! Form::close() !!}

2) change your route as below

Route::PUT('update/{id}','controller_name@update_method_name');

  • replace controller_name = Your Controller Name
  • replace update_method_name = Your Method name for update

Upvotes: 1

Try this:

RegViewUpdate.blade.php

<html>
<head>

<body>

<form action="edit/{{$users[0]->id}}" method="post" enctype="multipart/form-data">

{{ method_field('PUT') }}
{{ csrf_field() }}

<div class="form-group">
    <label>Name : *</label>
    <input type="text" class="form-control" name="name" value="{{$users[0]->name}}" required>
  </div>

  <div class="form-group">
    <label>Username : *</label>
    <input type="text" class="form-control" name="username" value="{{$users[0]->username}}" required>
  </div>

  <div class="form-group">
    <label>Password : *</label>
    <input type="password" class="form-control" name="password" value="{{$users[0]->pw}}" required>
  </div>

  <div class="form-group">
    <label>Upload Profile Picture :</label>
    <input type="file" class="form-control-file" name="file_img" aria-describedby="fileHelp">
    <small id="fileHelp" class="form-text text-muted">If U Want , U Can Skip Upload A Profile Picture</small>
  </div>

  <input type="submit" class="btn btn-primary" name="submit" value="Update">

</form>

</body>
</html>

RegViewController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;

class RegViewController extends Controller
{

    public function index()
    {
        return view('RegView');
    }

    public function show($id) {
        $users = DB::select('select * from academic where id = ?',[$id]);
        return view('RegViewUpdate',['users'=>$users]);
    }

    public function edit(Request $request, $id)
    {
        $name = $request->input('name');
        try {
             DB::table('academic')
                  ->where('id', $id)
                  ->update(['name' => $name]);
             echo "Record updated successfully.<br/>";
        }  catch (\Exception $ex) {
             dd($ex);
        }
    }

}

Routes:

Route::get('edit/{id}','RegViewController@show');
Route::put('edit/{id}','RegViewController@edit');

Upvotes: 3

iavery
iavery

Reputation: 554

I believe you will want to remove the following line:

{{ method_field('PUT') }}

The reason is that your Route is set up to use the POST method, so the PUT Route is not defined. Alternately, you could change it to specify the PUT method (which is redundant, because your specifies that already), or use the {{ Form::open() }} tag with the appropriate Route.

Upvotes: 1

Farsay
Farsay

Reputation: 312

it should be PUT method on route

Form action should be -

/edit/{{$users[0]->id}}

and route should be -

Route::put('/edit/{id}','RegViewController@edit');

Because you are using PUT method to send id.

Upvotes: 1

Related Questions