Home > Blockchain >  Storing array and only accepting arrays that have a value in them in Laravel
Storing array and only accepting arrays that have a value in them in Laravel

Time:12-28

How do I store arrays in laravel, for example, my database table only has firstname, middlename, and lastname in the column. and i have a field for for different family members in my form which shares the same column, so im planning to store the data as an array, the problem is, i cant store it. what is the problem in my code.

Ajax jquery code. I can get data in my ajax, but when it comes to the controller, it says undefined

$("#btn-save").click(function(e){
    e.preventDefault();
    var data = [
        {
            firstname: $('#father_firstName').val(),
            middlename: $('#father_middleName').val(),
            lastname: $('#father_lastName').val(),
        },
        {
            firstname: $('#mother_firstName').val(),
            middlename: $('#mother_middleName').val(),
            lastname: $('#mother_lastName').val(),
        },
    ];

Controller.php code

public function familyRegister(Request $request)
{
    // Initialize variables
    $data['user1'] = UserInfoModel::where('app_id',  '=', Session::get('loginId'))->first();
    $data['user2'] = FamilyMembersModel::where('seq_id', '=', Session::get('loginId'))->first();

    // Validate input
    $validator = Validator::make($request->all(), []);
    if ($validator->fails()) {
        return response()->json([
            'status' => 400,
            'errors' => $validator->messages(),
        ]);
    } else {
        $familyMembers = [];
        if ($request->input('father_firstName')) {
            $familyMembers[] = [
                'firstname' => $request->input('father_firstName'),
                'middlename' => $request->input('father_middleName'),
                'lastname' => $request->input('father_lastName'),   
            ];
        }
        if ($request->input('mother_firstName')) {
            $familyMembers[] = [
                'firstname' => $request->input('mother_firstName'),
                'middlename' => $request->input('mother_middleName'),
                'lastname' => $request->input('mother_lastName'),
            ];
        }
         
        foreach ($familyMembers as $member) {
            $record = new FamilyMembersModel;
            $record->firstname = $member['firstname'];
            $record->middlename = $member['middlename'];
            $record->lastname = $member['lastname'];
            $record->save();
        }
    }
}

CodePudding user response:

You are sending an array of objects. You can validate data like this: https://stackoverflow.com/a/58323845/12503693

Then you can access request data like this:

$data = $request->input();

$father_firstName = $data[0]['father_firstName'];
$father_middleName = $data[0]['father_middleName'];
$father_lastName= $data[0]['father_lastName'];

$mother_firstName = $data[1]['mother_firstName'];
$mother_middleName = $data[1]['mother_middleName'];
$mother_lastName= $data[1]['mother_lastName'];

OR

$father_firstName = $request->input('0.father_firstName');
$father_middleName = $request->input('0.father_middleName');
$father_lastName = $request->input('0.father_lastName');

$mother_firstName = $request->input('1.mother_firstName');
$mother_middleName = $request->input('1.mother_middleName');
$mother_lastName = $request->input('1.mother_lastName');
  • Related