Skip to content
Advertisement

keep getting an error while saving data into database in laravel

I am using laravel ver "laravel/framework": "5.8.*" I want to save the data into database with post request and my post data is

I have searched on this many topics but I can’t get any information on this specific topic. If anyone can, it will be very helpful to me. I saw my previous codes they this type of code in laravel but they work fine why this is not saving and getting an error.

{
       "userID": 11,
      "fullname": "dharmendra shah",
      "fatherName": "manohar shah",
      "Mothername": "manorama devi",
      "dobdate": "21",
      "dobmonth": "August",
      "dobYear": "1999",
      "caste": "OBC",
      "casteCertificateIs": "state",
      "emailAddress": "dharmonly1999@gmail.com",
      "gender": "Male",
      "handiCappedStatus": "Not handicapped",
      "nationality": "india",
      "state": "Rajasthan",
      "district": "bikaner",
      "tahsil": null,
      "village": "NA",
      "maritalStatus": "UnMarried",
      "spouseName": null,
      "children": null
}

In laravel route my controller is :

 public function store(Request $request)
{
    $this->validate($request, [
        'fullname' => 'required|max:25',
        'fatherName' => 'required|max:25',
        'Mothername' => 'required|max:25',
        'dobdate' => 'required',
        'dobmonth' => 'required',
        'dobmonth' => 'required',
        'dobYear' => 'required',
        'caste' => 'required',
        'casteCertificateIs'=>'required',
        'emailAddress' => 'required|email',
        'gender' => 'required',
        'handiCappedStatus' => 'required',
        'nationality' => 'required',
        'state' => 'required',
        'district' => 'required',
        'block' => 'sometimes|max:20',
        'tahsil' => 'sometimes|max:20',
        'village' => 'sometimes|max:20',
        'maritalStatus' => 'required',
        'spouseName' => 'sometimes|max:20',
        'children' => 'sometimes|max:5|integer'
    ]);

    $userInformationSave = new BauserInformation([
        'userID' => Auth::user()->id,
        'fullname' => $request->get('fullname'),
        'fatherName' => $request->get('fatherName'),
        'Mothername' => $request->get('Mothername'),
        'dobdate' => $request->get('dobdate'),
        'dobmonth' => $request->get('dobmonth'),
        'dobYear' => $request->get('dobYear'),
        'caste' => $request->get('caste'),
        'casteCertificateIs' => $request->get('casteCertificateIs'),
        'emailAddress' => $request->get('emailAddress'),
        'gender' => $request->get('gender'),
        'handiCappedStatus' => $request->get('handiCappedStatus'),
        'nationality' => $request->get('nationality'),
        'state' => $request->get('state'),
        'district' => $request->get('district'),
        'block' => $request->get('block'),
        'tahsil' => $request->get('tahsil'),
        'village' => $request->get('village'),
        'maritalStatus' => $request->get('maritalStatus'),
        'spouseName' => $request->get('spouseName'),
        'children' => $request->get('children')
    ]);

    $userInformationSave->save();
       return redirect()->route('home')->with('message', 'Your information is saved now');
}

The model is

class BauserInformation extends Model
{
    protected $table = ['userFinalInformation'];
    protected $fillable = ['userID', 'fullname', 'fatherName', 'Mothername', 'dobdate', 'dobmonth', 'dobYear', 'caste', 'casteCertificateIs', 'emailAddress', 'gender', 'handiCappedStatus', 'nationality', 'state', 'district', 'Block', 'tahsil', 'village', 'maritalStatus', 'spouseName', 'children'];
}

the error argument i am getting is

 ErrorException (E_NOTICE)
Array to string conversion

Advertisement

Answer

You are complicating things.

On top of your controller put: use IlluminateSupportFacadesValidator;.

Delete $userInformationSave.

Validate inputs:

$validator = Validator::make($request->all(), [
    'fullname' => ['required', 'max:25'],
    ... every other input like I have done
]);
if ($validator->fails()) {
    return redirect('/bla bla bla/create')->withErrors($validator);
}

And save the model:

BauserInformation::create($request->all());

EDIT #1 In you model, you should declare table and A_I like this:

protected $table = 'userFinalInformation';
protected $primaryKey = 'userID';

I think this will do the job.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement