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
foreachloop in some of your code snippets, but instead of using the$spousevalue, you're using aspouses()method? Or you're using the array$spousesinstead of the$spousevalue... what happens when you use$spouseinstead ofspouses()or$spouses?