Laravel教程:使用DB::update()方法更新数据记录

2018-10-0308:14:13后端程序开发Comments5,573 views字数 2158阅读模式
使用DB::update()方法更新记录。 DB::update()方法的语法如下表中所示。
语法int update(string $query, array $bindings = array())
参数
  • $query(string) – 在数据库中执行查询
  • $bindings(array) – 与查询绑定值
返回值int
描述
在数据库上运行一个更新语句

示例

第1步- 执行以下命令来创建一个名为 StudUpdateController 的控制器。
php artisan make:controller StudUpdateController
第2步 - 成功执行后,您会收到以下输出 -
Laravel教程:使用DB::update()方法更新数据记录

第3步 - 将以下代码复制到文件 - app/Http/Controllers/StudUpdateController.php文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6198.html

<?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 "更新记录成功.<br/>";
      echo '<a href = "/edit-records">点击这里</a> 返回';
   }
}
第4步 - 创建一个名为 resources/views/stud_edit_view.blade.php 的视图文件,并复制下面的代码到此文件中。

resources/views/stud_edit_view.blade.php文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6198.html

<html>
   <head>
      <title>查看学生记录</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 }}'>编辑</a></td>
         </tr>
         @endforeach
      </table>
   
   </body>
</html>
第5步 - 创建一个名为 resources/views/stud_update.php 的另一个视图文件,并复制下面的代码放入下面的文件中。

resources/views/stud_update.php文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6198.html

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6198.html

<html>
   
   <head>
      <title>编辑 | 学生管理</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>名字</td>
               <td>
                  <input type = 'text' name = 'stud_name' 
                     value = '<?php echo$users[0]->name; ?>'/>
               </td>
            </tr>
            <tr>
               <td colspan = '2'>
                  <input type = 'submit' value = "更新学生信息" />
               </td>
            </tr>
         </table>
      
      </form>
   
   </body>
</html>
第6步 - 添加以下行到文件 - app/Http/routes.php.

app/Http/routes.php文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6198.html

Route::get('edit-records','StudUpdateController@index');
Route::get('edit/{id}','StudUpdateController@show');
Route::post('edit/{id}','StudUpdateController@edit');
第7步 - 请访问以下网址数据库中更新记录。

http://localhost:8000/edit-records文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6198.html

第8步 - 输出结果如下图所示
Laravel教程:使用DB::update()方法更新数据记录
步骤9- 点击记录"编辑"链接,将被重定向到一个页面,可以编辑特定的记录。
第10步 - 输出如下面图所示。
Laravel教程:使用DB::update()方法更新数据记录
第11步 - 编辑这条记录后,会看到一个提示图如下图所示。
Laravel教程:使用DB::update()方法更新数据记录
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6198.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/6198.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定