如何解决Laravel中的此数据更新错误 [英] How To Solve This Data Update Error In Laravel

查看:97
本文介绍了如何解决Laravel中的此数据更新错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更新注册表数据.因此,首先,我为表演数据创建了一个表.在此表中,我包括一个ViewForUpdate按钮,并且传递了ID.当我单击该ViewForUpdate按钮时,它将在另一页中正确显示唯一数据.输入新数据后,当我单击更新"按钮时,它显示此错误-

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 ??

这是RegViewUpdate.blade.php文件

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>

这是RegViewController.php文件

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表结构.

推荐答案

尝试一下:

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);
        }
    }

}

路线:

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

这篇关于如何解决Laravel中的此数据更新错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆