简体   繁体   中英

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. The current output is an error message: Call to a member function update() on string.

The code I have used is shown below.

PageController@update function.

    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

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

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

My GitHub repository link is attached below if you want a better view of the code.

https://github.com/xiaoheixi/wfams

Thanks for the help everyone!

The error is coming from a non-instantiated object URI . You are passing a string from your form - whatever $pageContent->URI is. 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) ).

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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