为什么看起来“没有默认值错误”?

时间:2018-04-23 16:07:54

标签: laravel

我在下面有一个在ajax post请求中调用的storeRegistrationInfoF方法。如果没有验证错误,我想在注册表中创建一个条目。问题是我得到了这个错误:

message:
"SQLSTATE[HY000]: General error: 1364 Field 'participant_that_did_registration' doesn't have a default value (SQL: insert into `registrations` (`congress_id`, `status`, `updated_at`, `created_at`) values (1, C, 2018-04-23 16:02:53, 2018-04-23 16:02:53))"

你知道为什么吗?

public function storeRegistrationInfoF(Request $request, $id, $slug = null, Validator $validator){

        $user = Auth::user();

        $validator = Validator::make($request->all(),[
            'name' => 'required|max:255|string',
            'surname' => 'required|max:255|string',
            'email' => 'required|max:255|string',
            'participant_name.*' => 'required|max:255|string',
            'participant_surname.*' => 'required|max:255|string',
        ]);

        if($validator->passes())
        {
            $registration = Registration::create([
                'congress_id' => $id,
                'participant_that_did_registration', $user->id,
                'status' => 'C',

            ]);
            return response()->json([
                'success' => true,
                'message' => 'success'
            ], 200);
        }
        $errors = $validator->errors();
        $errors =  json_decode($errors);

        return response()->json([
            'success' => false,
            'errors' => $errors
        ], 422);
    }

//注册模式

class Registration extends Model
{
    protected $fillable = [
        'congress_id', 'status', 'participant_that_did_registration'
    ];

    // a registration has one user that do the registration
    public function customer(){
        return $this->belongsTo('App\User');
    }

    // a registration can have many participants
    public function participants(){
        return $this->hasMany('App\Participant');
    }
}

1 个答案:

答案 0 :(得分:3)

错字:

$registration = Registration::create([
    'congress_id' => $id,
    'participant_that_did_registration', $user->id, // <====
    'status' => 'C',

]);

你有一个逗号,你应该有=>

$registration = Registration::create([
    'congress_id' => $id,
    'participant_that_did_registration' => $user->id,
    'status' => 'C',

]);