简体   繁体   English

是否有使用laravel框架中的表单输入数据更新数据库中当前记录的功能?

[英]Is there a function to update the current record in the database with form input data in laravel framework?

I am having trouble running the following code to update my database with the form input the user has filled in. The ideal output would be to be redirected to my pageManagement.blade after updating the record in the database.我在运行以下代码以使用用户填写的表单输入更新我的数据库时遇到问题。理想的输出是在更新数据库中的记录后重定向到我的 pageManagement.blade。 The current output is an error message: Call to a member function update() on string.当前输出是一条错误消息:Call to a member function update() on string。

The code I have used is shown below.我使用的代码如下所示。

PageController@update function. PageController@update 函数。

    public function update($URI)
{
    $data = request()->validate([
        'title' => 'required',
        'URI' => 'required|min:5|max:10',
        'pageContent' => 'required'
    ]);
    $URI->update($data);
    return redirect('/p');
}

editPage.blade.php editPage.blade.php

<h1>Fill in the form to edit a page below.</h1>
<form action="/page/{{ $pageContent->URI }}" method="post">
@csrf
@method('PATCH')
  <label for="title">Title:</label><br>
  <input type="text" id="title" name="title" autocomplete="off" value="{{ $pageContent -> title     
}}"><br>
  @error('title') <p style="color: red">{{ $message }}</p> @enderror
  <label for="URI">URI:</label><br>
  <input type="text" id="URI" name="URI" autocomplete="off" value="{{ $pageContent -> URI }}">    
<br>
  @error('URI') <p style="color: red">{{ $message }}</p> @enderror
  <label for="pageContent">Page Content:</label><br>
  <textarea id="pageContent" name="pageContent" value="{{ $pageContent -> pageContent }}"> 
</textarea>
  @error('pageContent') <p style="color: red">{{ $message }}</p> @enderror
  <input type="submit" value="Submit">
</form>

THE SCRIPT SRC SHOULD BE HERE BUT HAS NOT BEEN INCLUDED DUE TO THE INDENTATION ISSUE WITH     
STACKOVERFLOW.
<script>
tinymce.init({
    selector:'#pageContent'
})
</script>

Web.php file where I store my routes.我存储路由的 Web.php 文件。

Route::patch('/page/{URI}','PageController@update');

My GitHub repository link is attached below if you want a better view of the code.如果您想更好地查看代码,请在下面附上我的 GitHub 存储库链接。

https://github.com/xiaoheixi/wfams https://github.com/xiaoheixi/wfams

Thanks for the help everyone!感谢大家的帮助!

The error is coming from a non-instantiated object URI .错误来自非实例化对象URI You are passing a string from your form - whatever $pageContent->URI is.您正在从表单传递一个字符串 - 无论$pageContent->URI是什么。 Without loading the object you wish to update, it is just a string, thus the error message.没有加载你想要更新的对象,它只是一个字符串,因此是错误消息。

$URI->update($data);

at the end of your method is basically saying something like 'call update on the number 32' ( 32->update($data) ).在您的方法结束时,基本上是在说“在数字 32 上调用更新”( 32->update($data) )。

To fix the error in your question, you can instantiate the object at the beginning of your method:要修复问题中的错误,您可以在方法的开头实例化对象:

 public function update($URI)
{
  $data = request()->validate([
    'title' => 'required',
    'URI' => 'required|min:5|max:10',
    'pageContent' => 'required'
  ]);
  $obj = \App\URIModel::find($URI); // Whatever the model is
  $obj->update(request()->all());
  return redirect('/p');
}

Alternate Fix : Since you have the named variable inside the route file, you can also take advantage of route-model binding by injecting the model directly in the method() to save a line of code:替代修复:由于您在路由文件中有命名变量,您还可以通过在 method() 中直接注入模型来利用路由模型绑定来节省一行代码:

public function update(\App\URIModel $URI)
{
  $data = request()->validate([
    'title' => 'required',
    'URI' => 'required|min:5|max:10',
    'pageContent' => 'required'
  ]);
  $URI->update(request()->all);
  return redirect('/p');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM