Laravel资源控制器中的Update()函数无法正常工作

时间:2019-11-15 09:11:04

标签: php laravel

因此,我尝试使用update()函数更新选定的用户,但是当我单击“提交”时,它只是返回到索引(应该如此),但是什么也不更新。以下是我的代码:

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()行中。而且编辑页面甚至在输入字段上显示我的旧数据,但是当我进行更改并单击“更新”时,它只是不更新​​数据库或索引页面中的任何内容-那我在做什么错呢?在此先感谢您,请随时询问是否需要更多代码来破解该密码。

4 个答案:

答案 0 :(得分:4)

尝试一下:

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!');
}

答案 1 :(得分:2)

尝试..

   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!');
    }

答案 2 :(得分:1)

在您的edit.blade.php中,

{!! Form::submit('route_to_update()',['type'=>'submit','class'=>'btn btn-danger btn-block']) !!}  

更改“编辑” 您可以在控制台中通过“ php artisan route:list之类的

来检查您的路线

并检查您的路线是否到达 通过var_damp()var_export()dd()使用学生控制器更新功能

答案 3 :(得分:1)

request()->all()包含方法和令牌信息。您可以通过验证程序吗?

$validated = request()->validate([
'name' => 'required',
'address' => '',
..
]);
$student->update($validated);