目录

Update Records

我们可以使用update方法使用DB facade update记录。 update方法的语法如下表所示。

Syntax int update(string $ query,array $ bindings = array())
Parameters
  • $ query(string) - 在数据库中执行的查询
  • $ bindings(array) - 要与查询绑定的值
Returnsint
Description 对数据库运行更新语句。

例子 (Example)

请注意以下示例以了解有关更新记录的更多信息 -

Step 1 - 执行以下命令以创建名为StudViewController的控制器。

php artisan make:controller StudUpdateController --plain

Step 2 - 成功执行后,您将收到以下输出 -

更新记录

Step 3 - 将以下代码复制到文件app/Http/Controllers/ StudUpdateController.php

app/Http/Controllers/StudUpdateController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class StudUpdateController extends Controller {
   public function index() {
      $users = DB::select('select * from student');
      return view('stud_edit_view',['users'=>$users]);
   }
   public function show($id) {
      $users = DB::select('select * from student where id = ?',[$id]);
      return view('stud_update',['users'=>$users]);
   }
   public function edit(Request $request,$id) {
      $name = $request->input('stud_name');
      DB::update('update student set name = ? where id = ?',[$name,$id]);
      echo "Record updated successfully.<br/>";
      echo '<a href = "/edit-records">Click Here</a> to go back.';
   }
}

Step 4 - 创建一个名为的视图文件

resources/views/stud_edit_view.blade.php并在该文件中复制以下代码。

resources/views/stud_edit_view.blade.php

<html>
   <head>
      <title>View Student Records</title>
   </head>
   <body>
      <table border = "1">
         <tr>
            <td>ID</td>
            <td>Name</td>
            <td>Edit</td>
         </tr>
         @foreach ($users as $user)
         <tr>
            <td>{{ $user->id }}</td>
            <td>{{ $user->name }}</td>
            <td><a href = 'edit/{{ $user->id }}'>Edit</a></td>
         </tr>
         @endforeach
      </table>
   </body>
</html>

Step 5 - 创建另一个名为的视图文件

resources/views/stud_update.php并在该文件中复制以下代码。

resources/views/stud_update.php

<html>
   <head>
      <title>Student Management | Edit</title>
   </head>
   <body>
      <form action = "/edit/<?php echo $users[0]->id; ?>" method = "post">
         <input type = "hidden" name = "_token" value = "<?php echo csrf_token(); ?>">
         <table>
            <tr>
               <td>Name</td>
               <td>
                  <input type = 'text' name = 'stud_name' 
                     value = '<?php echo$users[0]->name; ?>'/>
               </td>
            </tr>
            <tr>
               <td colspan = '2'>
                  <input type = 'submit' value = "Update student" />
               </td>
            </tr>
         </table>
      </form>
   </body>
</html>

Step 6 - 在app/Http/routes.php. Add以下行app/Http/routes.php.

app/Http/routes.php.

Route::get('edit-records','StudUpdateController@index');
Route::get('edit/{id}','StudUpdateController@show');
Route::post('edit/{id}','StudUpdateController@edit');

Step 7 - 访问以下URL以更新数据库中的记录。

http://localhost:8000/edit-records

Step 8 - 输出将如下图所示。

编辑记录

Step 9 - 单击任何记录上的编辑链接,您将被重定向到可以编辑该特定记录的页面。

Step 10 - 输出将如下图所示。

特别记录

Step 11 - 编辑该记录后,您将看到如下图所示的提示。

记录更新
↑回到顶部↑
WIKI教程 @2018