Php Laravel资源控制器中的Update()函数不工作
所以我尝试使用update函数来更新所选用户,但当我单击submit时,它会返回到index,但不会更新任何内容。以下是我的代码: StudentController资源控制器:Php Laravel资源控制器中的Update()函数不工作,php,laravel,Php,Laravel,所以我尝试使用update函数来更新所选用户,但当我单击submit时,它会返回到index,但不会更新任何内容。以下是我的代码: StudentController资源控制器: <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Student; class StudentController extends Controller { /** * Display
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Student;
class StudentController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$students = Student::all();
return view ('main',compact('students'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view ('create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
Student::create($request->all());
return redirect()->route('main.index')->with('create','Student has been added successfully!');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id, Student $student)
{
$student = Student::findOrFail($id);
return view ('edit',compact('student'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Student $student)
{
$student->update($request->all());
return redirect()->route('main.index')->with('update','Student has been updated!');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
Main.blade.php索引:
edit.blade.php模型:
Student.php模型:
我的create.blade.php与我的edit.blade.php完全相同,唯一的更改是表单::openline。编辑页面甚至会在输入字段中显示我的旧数据,但当我进行更改并单击更新时,它不会更新数据库或索引页面中的任何内容-那么我做错了什么?提前感谢,请随时询问是否需要更多代码来破解此版本。尝试此版本:
public function update(Request $request, Student $student)
{
$input = $request->all();
$student->fill($input)->save();
return redirect()->route('main.index')->with('update','Student has been updated!');
}
试试这个:
public function update(Request $request, Student $student)
{
$input = $request->all();
$student->fill($input)->save();
return redirect()->route('main.index')->with('update','Student has been updated!');
}
在edit.blade.php中
更改编辑
您可以通过在控制台中像php artisan route:list一样检查路由
检查你的路线是否到达
通过var_damp或var_export或ddin在edit.blade.php中的学生控制器更新函数中
更改编辑
您可以通过在控制台中像php artisan route:list一样检查路由
检查你的路线是否到达
通过var_damp或var_export或ddin,在学生控制器更新功能中请求->所有包含方法和令牌信息。你能把它通过验证器吗
$validated = request()->validate([
'name' => 'required',
'address' => '',
..
]);
$student->update($validated);
请求->全部包含方法和令牌信息。你能把它通过验证器吗
$validated = request()->validate([
'name' => 'required',
'address' => '',
..
]);
$student->update($validated);
试试
试试
更新时没有错误吗?显示dd$request->all的输出;从更新功能中,当您添加$student时,您得到了什么?是否显示更新警报?尝试在更新模型之前和之后添加$student->getAttributes,看看它是否发生了变化$学生->更新$request->全部;dd$student->getAttributes;return redirect->route'main.index'->带有'update','Student has updated!';它返回一个空数组[]更新时没有错误吗?显示dd$request->all的输出;从更新功能中,当您添加$student时,您得到了什么?是否显示更新警报?尝试在更新模型之前和之后添加$student->getAttributes,看看它是否发生了变化$学生->更新$request->全部;dd$student->getAttributes;return redirect->route'main.index'->带有'update','Student has updated!';它返回一个空数组[]@VenoM您应该只使用['name'、'gender'、'address'、'class'],而不是获取每个值和您的类型提示是正确的,但您只是删除了它:@VenoM您应该只使用['name'、'gender'、'address'、'class'],而不是获取每个值,您的类型提示是正确的,但您只是删除了它:!
{!! Form::submit('route_to_update()',['type'=>'submit','class'=>'btn btn-danger btn-block']) !!}
$validated = request()->validate([
'name' => 'required',
'address' => '',
..
]);
$student->update($validated);
public function update(Request $request,$id)
{
$student = Student::find($id);
$student->name = $request->input('name');
$student->gender = $request->input('gender');
$student->address = $request->input('address');
$student->class = $request->input('class');
$student->save();
return redirect()->route('main.index')->with('update','Student has been updated!');
}