php 我尝试使用update()laravel更新数据库记录

nlejzf6q  于 2023-08-02  发布在  PHP
关注(0)|答案(2)|浏览(111)

控制器:

public function updateTeachers(Request $request, User $teachers){
  $validated = $request->validate([
      "name" => ['required'],
      "email" => ['required'],

  ]);

  $teachers->update($validated);

  return back()->with('information-message', 'Data was successfully updated');

字符串
}

查看:

<form action="/teacher/{{ $dataTeacher->id }}" method="POST" class="bg-white divide-y dark:divide-gray-700 dark:bg-gray-80 px-4 py-3">
      @method('PUT')
      @csrf
      <div class="flex flex-wrap -mx-3 mb-6">
        <div class="w-full px-3">
          <label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-first-name">
            Name
          </label>
          <input type="text" name="name" value="{{ $dataTeacher->name }}" class="appearance-none block w-full bg-gray-200 text-gray-700 border border-gray-200 rounded py-3 px-4 mb-3 leading-tight focus:outline-none focus:bg-white focus:border-gray-500" required>
          
        </div>

      </div>
      <div class="flex flex-wrap -mx-3 mb-6">
        <div class="w-full px-3">
          <label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-password">
            Email
          </label>
          <input type="email" name="email" value="{{ $dataTeacher->email}}" class="appearance-none block w-full bg-gray-200 text-gray-700 border border-gray-200 rounded py-3 px-4 mb-3 leading-tight focus:outline-none focus:bg-white focus:border-gray-500" required>
      
        </div>
      </div>
      <button type="submit" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5   text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">Push</button>

路线:

Route::put('/teacher/{teacher}', [UserController::class, 'updateTeachers']);


我的更新代码是否有误?它不工作,它只显示信息-消息
如何在laravel中使用更新?

vwkv1x7d

vwkv1x7d1#

其实有很多方法可以更新它。
首先,确保您的模型已将“name”和“email”添加到fillable属性。
第二步:

$teachers->update([
   'name' => 'John Doe',
   'email' => 'john@test.com',
]);

字符串
第三步:

$data = $teachers;
$data->name = $request->name;
$data->email= $request->email;
$data->save();


第四步:

use Illuminate\Support\Facades\DB;

DB::table('users')->where('id', $teachers->id)->update([
        'name' => 'John Doe',
       'email' => 'john@test.com',
]);


其中一个可能对你有帮助。试试这些方法。
快乐编程

flseospp

flseospp2#

Route::put('/teacher/{user}', [UserController::class, 'updateTeachers']);

字符串


的数据

相关问题