首页 > 解决方案 > createorupdate 功能不起作用。填充模型缺少参数

问题描述

传递给 Illuminate\Database\Eloquent\Model::fill() 的参数 1 必须是数组类型,给定 null,

public function createOrUpdate(Staff $staff, Request $request)
{
    $staff->fill($request->get('staff'))->save();
   
    $staff->attachment()->syncWithoutDetaching(
        $request->input('staff.attachment', [])
    );

    Alert::info('You have successfully Added a Member.');

    return redirect()->route('platform.staff');
}

标签: phplaravel

解决方案


key => value在您的 fill() 方法中提供一个关联数组

public function createOrUpdate(Staff $staff, Request $request)
{
    $staff->fill(['column_name' => $request->get('staff')])->save();
   
    $staff->attachment()->syncWithoutDetaching(
        $request->input('staff.attachment', [])
    );

    Alert::info('You have successfully Added a Member.');

    return redirect()->route('platform.staff');
}

注意:确保您protected $fillable有财产

https://laravel.com/docs/8.x/eloquent#mass-assignment


推荐阅读