user9659025
user9659025

Reputation:

Why it appears this "doesn´t have a default value error"?

I have this storeRegistrationInfoF method below that is called in an ajax post request. If there is not validation errors I want to create an entry in the registration table. The issue is that Im getting this error:

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))"

Do you know why?

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

// Registration Model

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

Upvotes: 0

Views: 29

Answers (1)

DevK
DevK

Reputation: 9962

Typo here:

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

]);

You have a comma where you should have =>:

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

]);

Upvotes: 2

Related Questions