0

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();
        }
    }
}
5
  • what does the result if you use dd($request->all()) ? Commented Dec 23, 2022 at 1:50
  • array:1 [ // app\Http\Controllers\CustomAuthController.php:1942 "undefined" => null ] or did i just dd at the wrong place? Commented Dec 23, 2022 at 1:56
  • @CaptainCRAZY the issue is on the JS side, you are sending "undefined" string to the server, not your data Commented Dec 23, 2022 at 3:52
  • What should i do or change @DmitryK.? Commented Dec 27, 2022 at 0:27
  • @CaptainCRAZY what's your question? Did you see answer to your question? Commented Dec 27, 2022 at 5:49

1 Answer 1

1

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');
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, i'll read the documentation first
Sorry, i gave wrong link. Now it is correct link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.