首页 > 解决方案 > 不更新数据库也不在 laravel 中抛出错误

问题描述

更新用户时遇到问题。当我在单击保存按钮后尝试更新用户时,它会将我重定向到同一页面并且不会向我抛出任何错误,但也不会更新数据库中的任何内容。下面是我的代码。我不知道这里发生了什么。帮我 :)

控制器

public function update(ReportRequest $request, $id)
{
    $report = Report::findOrFail($id);
    $input = $request->all();

    if ($file = $request->file('photo_id')) {
        $name = time() . $file->getClientOriginalName();
        $file->move('images', $name);
        $photo = Photo::create(['file' => $name]);
        $input['photo_id'] = $photo->id;
    }

    $report->update($input);

    return redirect()->back();
}

路线

Route::resource('admin/reports', 'ReportController', ['names'=>[

    'index'=>'admin.reports.index',
    'create'=>'admin.reports.create',
    'edit'=>'admin.reports.edit',

]]);

楷模

class Report extends Model
{
    protected $fillable = [
        'student_id',
        'student_name',
        'class_id',
        'subject',
        'teacher_name',
        'report_categories_id',
        'total_marks',
        'obtained_marks',
        'percentage',
        'position',
        'photo_id',
    ];

    public function photo() {
        return $this->belongsTo('App\Photo');
    }

    public function studentsClass() {
        return $this->belongsTo('App\StudentsClass', 'class_id');
    }

    public function student() {
        return $this->belongsToMany('App\Student');
    }


}

标签: laravel

解决方案


确保在and模型中有$fillable属性,否则and方法将无法按预期工作。PhotoReportcreate()update()


推荐阅读