1

In m app I have a form with data meant to insert data into 2 tables :payers and Spouses and Payer has Many Spouse. In my PayerController i have

public function store(CreatePayerRequest $request)
    { 
        $input = $request->all();
        $payer = $this->payerRepository->create($input);
        {

and i have

dd($request->all())

and got the following result showing that my array is passing successfully from my input form.

array:34 [▼
  "_token" => "LDdsaesoaRlKmM0URHVxQqyiJHdrPhLz3lmYQs6V"
  "added_on" => "2020-06-28 11:07:39"
  "user_id" => array:2 [▼
    0 => "Auth::user()->id"
    1 => "Auth::user()->id"
  ]
  "ass_code" => "508063"
  "title" => "Mr"
  "surname" => "PLC"
  "firstname" => "ZENITH"
  "othernames" => "A,"
  "dob" => "2020-06-28 12:08:59"
  "address" => "43"
  "street" => "Old School"
  "town" => "Umuguma"
  "lga" => "Okigwe"
  "state" => "Imo"
  "nationality" => "Nigerian"
  "occupation" => "Civil Servant"
  "vocation" => "Employment"
  "phone" => "8035514494"
  "email" => "[email protected]"
  "income_status" => "Yes"
  "allowance_status" => "No"
  "acc_status" => "1"
  "assess_status" => "0"
  "approve_status" => "0"
  "payment_status" => "0"
  "authorize_status" => "0"
  "marital_stat" => "Married"
  "spouse_name" => array:2 [▼
    0 => "Tony"
    1 => "Nkechi"
  ]
  "spouse_dob" => array:2 [▶]
  "spouse_emplbiz_add" => array:2 [▼
    0 => "nA"
    1 => "nA"
  ]
  "spose_income" => array:2 [▼
    0 => "50000"
    1 => "50000"
  ]
  "child_status" => "Yes"
  "vehicle_status" => "Yes"
  "assets_status" => "Nigeria"
]

My problem is that when i send the form with the spouse array it gives me error:

ErrorException Array to string conversion

I have tried various codes eg

Code 1

 $payer->spouses()->create();
       

Code 2

 $spouses = Spouse::find($payer->id);
            foreach($request->spouses as $spouse){
                $payer->spouses()->create([
                    'user_id' => Auth::user()->id,
                    'spouse_occupation'=>NA,
                    'spouse_name'=>$spouse_name,
                    'spouse_dob'=>$spouse_dob,
                    'spouse_emplbiz_add'=>$spouse_emplbiz_add]);

Code 3:

$spouses = new Spouse();
                $spouses->user_id = Auth::user()->id;
                $spouses->payer_id = $payer->id;
                $spouses->spouse_occupation='NA';
                $spouses->spouse_name = $request->input('spouse_name');
                $spouses->spouse_dob = $request->input('spouse_dob');
                $spouses->spouse_emplbiz_add = $request->input('spouse_emplbiz_add');
                $spouses->spose_income = $request->input('spose_income');
                $spouses->save();

And i keep getting same error.

Can anybody show me how to insert the Spouse detail in the array into my database at the same time as i am inserting the Payer.

In the tutorial I am following, the tutor send the following codes but its not working for me:

foreach($request->spouses as $spouse){
payer->spouses()->(['name'='$spouse'])

I am thinking its cos he had only one inputfield. But in my case i have up to 4.

My Spouse input is a livewire component with the codes below

@foreach($spouses as $spouse)
   <div class="col-md-10 d-flex">
        
 {!! Form::hidden('user_id[]', 'Auth::user()->id',array(
    'class' => 'form-control col-3',
    'id' => 'user_id[]',
))  !!}
   {!! Form::text('spouse_name[]', '',array(
        'class' => 'form-control col-3',
        'id' => 'spouse_name[]',
        'placeholder' => 'Spouse name',
    ))  !!}
    {!! Form::text('spouse_dob[]', '',array(
        'class' => 'form-control col-3',
        'id' => 'spouse_dob[]',
        'placeholder' => 'Spouse Age',
    ))  !!}
    {!! Form::text('spouse_emplbiz_add[]', '',array(
        'class' => 'form-control col-3',
        'id' => 'spouse_emplbiz_add[]',
        'placeholder' => 'Work Address',
    ))  !!}
    {!! Form::text('spose_income[]', '',array(
        'class' => 'form-control col-3',
        'id' => 'spose_income[]',
        'placeholder' => 'Gross Income',
    )) !!}
    <span class="btn btn-default fa fa-times text-danger padding:2" 
    wire:click="remove({{$loop->index}})"></span>        
</div>
    @endforeach
2
  • The code $payer = $this->payerRepository->create($input); runs and inserts payers details into db. It is in the next stage of adding the spouse array that i get the error, I am guessing since my spouse data is an array it must be entered in a specific format. Check the 3 codes above and you will see their create functions individually. Commented Jun 28, 2020 at 14:16
  • You use a foreach loop in some of your code snippets, but instead of using the $spouse value, you're using a spouses() method? Or you're using the array $spouses instead of the $spouse value... what happens when you use $spouse instead of spouses() or $spouses? Commented Jun 28, 2020 at 16:30

2 Answers 2

1

Your error accurately describes your problem. You are trying to save the array and therefore you getting this error.

$spouses->spouse_name = $request->input('spouse_name');
            $spouses->spouse_dob = $request->input('spouse_dob');
            $spouses->spouse_emplbiz_add = $request->input('spouse_emplbiz_add');
            $spouses->spose_income = $request->input('spose_income');

All these data are arrays.

"spouse_name" => array:2 [▼
  0 => "Tony"
  1 => "Nkechi"
]
"spouse_dob" => array:2 [▶]
"spouse_emplbiz_add" => array:2 [▼
  0 => "nA"
  1 => "nA"
]
"spose_income" => array:2 [▼
  0 => "50000"
  1 => "50000"
]

if the type of these columns in your table is json, then you should save it like that.

$spouses->spouse_name = json_encode($request->input('spouse_name'));
    $spouses->spouse_dob = json_encode($request->input('spouse_dob'));
    $spouses->spouse_emplbiz_add = json_encode($request->input('spouse_emplbiz_add'));
    $spouses->spose_income = json_encode($request->input('spose_income'));
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your answer. It stopped the error message though didnt insert the data. I modified my code in line with your advice $input = json_encode($request->all()); $payer = $this->payerRepository->create($input); and now its missing user_id which has been there even when u DD. 'user_id' doesn't have a default value. What do u think is best to do,
if your columns user_id, spouse_name, spouse_dob, spouse_emplbiz_add, spose_income do not have a json type, then my best advice is to change their types to json, this will fix a lot of problems, and you will only need to save this data user_id, spouse_name, spouse_dob, spouse_emplbiz_add, spose_income like this json_enocde($some_data), and the rest of the data, which is not an array, must be saved in the usual way $spouses->ass_code = $request->input('ass_code'), $spouses->title = $request->input('title'), ...,
another approach may be a little more complicated and take a lot of memory in your database, that means you have to find the largest array in your data, go through with a foreach, and save all the data for example for spouse_name column, and for the rest of the columns, which not arrays, save the same data, but I do not recommend this method.
When i have used this code in my controller doesnt it encode all including the user_id? $input = json_encode($request->all());
you shouldn't do json_encode($request->all()), you must call json_encode() only for data that is an array, like user_id, spouse_name, spouse_dob, spouse_emplbiz_add, spose_income, but this will only work if these columns in your table have json type.
0

you can try something like this for payer and spouse model ....

    $payer = new Payer();
    $payer->property =  $request->property;
    $payer->save();

if(count($request->spouse_name) > 0){
    for($i = 0; $i < count($request->spouse_name); $i++){

        $object = new Spouse();
        $object->payer_id = $payer->id
        $object->spouse_name= $request->spouse_name[$i];
        $object->save();

   } 
}

6 Comments

This answer is missing its educational explanation. Please always explain your answers ...always. Why ask php to count the same thing over and over? Why not just use a foreach and remove all of the count calls?
i know ... but when insert and update data simultaneously .. it think it's better solution ... if any suggestion here please give me
I fail to see any advantage in making the same count() call over and over and over only to receive the same result. Just doesn't make logical sense. This is not advanced coding.
( Product and ProductImage Model ) : sometimes i do not need insert image or multiple images for product model ... this situation i need to check if have any sub images then i will do operation ...
Who said anything about inserting images? Are you referring to a different question? Foreach will not iterate an empty array. Are you new to php?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.