因此,我试图使用update()函数更新所选用户,但是当我单击Submit时,它只是返回索引(应该如此),但是什么也不更新。以下是我的代码:
StudentController(资源控制器):
<?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(索引):
<div class="container">
<div class="row">
<div class="col-lg-12">
<div class="py-4">
@if (Session::has('create'))
<div class="alert alert-info">
{{ Session::get('create') }}
</div>
@endif
@if (Session::has('update'))
<div class="alert alert-info">
{{ Session::get('update') }}
</div>
@endif
<div class="card">
<div class="card-header">
Students
{{ link_to_route('main.create','Add Student','',['class'=>'btn btn-success float-right']) }}
</div>
<div class="card-body">
<table id="myTable" class="table table-striped table-bordered">
<thead>
<tr>
<th>Student Name</th>
<th>Gender</th>
<th>Address</th>
<th>Class</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach($students as $student)
<tr>
<td>{{ $student->name }}</td>
<td>{{ $student->gender }}</td>
<td>{{ $student->address }}</td>
<td>{{ $student->class }}</td>
<td>{{ link_to_route('main.edit','Edit',[$student->id],['class'=> 'btn btn-primary btn-sm']) }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
edit.blade.php(模型):
{!! Form::model($student,array('route'=>['main.update',$student->id],'method'=>'PUT')) !!}
<div class="form-group">
{!! Form::text('name',null,['class'=>'form-control','placeholder'=>'Add Student Name']) !!}
</div>
<div class="form-group">
{!! Form::select('gender', [
'Male' => 'Male',
'Female' => 'Female'],
null, ['class'=>'custom-select','placeholder' => 'Choose Gender']); !!}
</div>
<div class="form-group">
{!! Form::text('address',null,['class'=>'form-control','placeholder'=>'Add Student Address']) !!}
</div>
<div class="form-group">
{!! Form::select('class', [
'A' => 'A',
'B' => 'B',
'C' => 'C',
'D' => 'D',],
null, ['class'=>'custom-select','placeholder' => 'Choose Class']); !!}
</div>
<div class="form-group py-4">
{!! Form::submit('Edit',['type'=>'submit','class'=>'btn btn-danger btn-block']) !!}
</div>
{!! Form::close() !!}
Student.php(模型):
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
protected $fillable = ['name','gender','address','class'];
}
我的create.blade.php与我的edit.blade.php完全相同,唯一的变化是在Form :: open()行中。而且编辑页面甚至在输入字段上显示我的旧数据,但是当我进行更改并单击“更新”时,它只是不更新数据库或索引页面中的任何内容-那我在做什么错呢?在此先感谢,请随时询问是否需要更多代码来破解此代码。
尝试这个:
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!');
}
您正在将$student->id
传递给控制器。不是控制器函数中的类对象。.so
public function update(Request $request,$id)
{
$student = Student::find($id);
$student->gender = $request->input('gender');
$student->address = $request->input('address');
$student->class = $request->input('class');
//updated whichever fields to be updated
$student->save();
return redirect()->route('main.index')->with('update','Student has been updated!');
}