I want to insert multiple checkbox values and more than one email I am doing it with foreach loop. Everything is working fine but when 1 email is empty. It's inserting null in the email field and also null checkbox is also inserting full data.
it is my form with 20 inputs
<form method="POST" action="{{route('send.document')}}">
@csrf
// 1st Email
<input type="email" name="email[]">
<input type="checkbox" name="task[]" value="sign">
<input type="checkbox" name="task[]" value="initial">
<input type="checkbox" name="task[]" value="date">
<input type="checkbox"name="task[]" value="cc">
// 2nd Email
<input type="email" name="email[]" >
<input type="checkbox"name="task[]" value="sign">
<input type="checkbox" name="task[]" value="initial">
<input type="checkbox" name="task[]"value="date">
<input type="checkbox" name="task[]" value="cc">
// 3rd Email
<input type="email" name="email[]">
<input type="checkbox" name="task[]" value="sign">
<input type="checkbox" name="task[]" value="initial">
<input type="checkbox" name="task[]" value="date">
<input type="checkbox" name="task[]" value="cc">
// 4th Email
<input type="email" name="email[]" >
<input type="checkbox" name="task[]" value="sign">
<input type="checkbox" name="task[]" value="initial">
<input type="checkbox" name="task[]" value="date">
<input type="checkbox" name="task[]" value="cc">
<button type="submit">Send</button>
</form>
and it is my route
Route::post('/send-document',[SendDocumentController::class, 'sendDocToUser'])-
>name('send.document');
my controller
public function sendDocToUser(Request $request)
{
$UpdateFile = FileStorage::where('id', $request->id)->first();
if (!empty($request->email)) {
foreach($request->email as $mail) {
$save = new SendFile([
'Email' => $mail,
'senderId' => Auth::id(),
'fileName'=> $UpdateFile->uniqueFileName,
'task' => $request->task,
]);
$save->save();
}
}
}
database inserted data:
CodePudding user response:
I would restructure your <form>
slightly to incorporate a key
on the task[]
checkbox
es and to also leverage looping.
<form method="POST" action="{{ route('fake.store') }}">
@csrf()
@for ($i = 0; $i < 3; $i )
<input type="email" name="email[]" />
<input type="checkbox" name="task[{{$i}}][]" value="sign" />
<input type="checkbox" name="task[{{$i}}][]" value="initial" />
<input type="checkbox" name="task[{{$i}}][]" value="cc" />
<input type="checkbox" name="task[{{$i}}][]" value="date" />
@endfor
<button>Submit</button>
</form>
You could if you wanted pass the upper limit for $i
into the view as a parameter from your Controller
function (see sandbox at the end for this).
This will submit your task
checkbox
es as an array
but with an index of $i
which we will use as a hook to remove based on whether the email[]
field with the corresponding index is null
.
public function store(Request $request)
{
$notNullEmails = array_filter($request->email);
$merged = [];
foreach ($notNullEmails as $key => $value) {
$merged[] = ['email' => $notNullEmails[$key], 'task' => $request->task[$key]];
}
dump($merged);
}
The result of the above being:
array:2 [▼
0 => array:2 [▼
"email" => "[email protected]"
"task" => array:2 [▼
0 => "initial"
1 => "cc"
]
]
1 => array:2 [▼
"email" => "[email protected]"
"task" => array:2 [▼
0 => "sign"
1 => "date"
]
]
]
Here is a functional example on PHPSandbox.io.