Form Submission Repository
To work with the Form Submissions Repository, use the following Facade:
use Statamic\Facades\FormSubmission;
Methods#
| Methods | Description |
|---|---|
all() |
Get all form submissions, including partial and spam submissions. |
whereForm($handle) |
Get submissions by form handle, including partial and spam submissions. |
whereInForm($handles) |
Get submissions, across multiple forms. Accepts an array of form handles. |
find($id) |
Get a form submission, by its submission ID. |
make() |
Makes a new Submission instance |
query() |
Query Builder |
These methods return submissions of every status. $form->submissions() and the form's submission count only include finalized submissions. Use $form->querySubmissions() if you need the others.
Querying#
Examples#
Get form submissions by form#
FormSubmission::whereForm('postbox');
Get form submissions, between multiple forms#
FormSubmission::whereInForm(['postbox', 'newsletter']);
Get a single submission to a form by its id#
FormSubmission::find($id);
Get form submissions, filtered by field#
FormSubmission::query()
->where('form', 'postbox')
->get();
Get form submissions by status#
FormSubmission::query()
->where('form', 'postbox')
->whereStatus('partial') // or 'finalized', 'spam' or 'any'
->get();
whereStatus() is available on the default (Stache) submission query builder, but isn't part of the query builder contract, so custom drivers may not support it. Partial submissions have a _partial key set to true, and spam submissions have a _marked_spam_at timestamp, so you can query those directly instead.
Creating#
Start by making an instance of a form submission with the make method.
You need to pass in a Form instance before you can save a form submission.
$form = \Statamic\Facades\Form::find('postbox');
$submission = FormSubmission::make()->form($form);
To set submission data, you may call the ->data() method and pass an array:
$submission->data([
'name' => 'David Hasselhoff',
]);
Finally, save it. It'll return a boolean for whether it succeeded.
$submission->save(); // true or false
Partial Submissions#
A partial submission is an incomplete submission that's been saved before the user has finished filling out the form.
They're primarily used by the Forms Pro add-on to persist progress between the pages of a multi-page form, but add-ons and custom code can use them too.
To mark a submission as partial, call asPartial() before saving:
$submission = FormSubmission::make()->form($form);
$submission->data(['name' => 'David Hasselhoff']);
$submission->asPartial()->save();
You can check whether a submission is partial using isPartial(), or by getting its status() (which returns partial, finalized or spam):
$submission->isPartial(); // true
$submission->status(); // "partial"
Partial submissions are automatically deleted after a configurable number of days (7 by default).
Saving a partial submission only dispatches the SubmissionSaving and SubmissionSaved events. The SubmissionCreating and SubmissionCreated events aren't dispatched until the submission is finalized.
Finalizing#
When a submission is complete, call finalize() on it. You don't need to call save() yourself. It:
- Removes its partial status and converts any uploaded files into assets.
- Dispatches the
SubmissionCreatingevent. If a listener returnsfalse, the submission isn't stored, but finalizing carries on. - Saves the submission, then dispatches
SubmissionCreated. - Dispatches
SubmissionFinalized. - Triggers the form's connections.
If storage is disabled for the form, SubmissionCreating doesn't fire. Instead, SubmissionCreated fires and the partial submission is deleted, before carrying on with SubmissionFinalized and the connections.
Calling finalize() on a submission that isn't partial does nothing.
$submission->finalize();
Spam Submissions#
A submission can be marked as spam, which hides it from the submissions listing by default and excludes it from submission counts and limits.
$submission->markAsSpam()->save();
$submission->isSpam(); // true
$submission->status(); // "spam"
To remove the flag again, call markAsNotSpam():
$submission->markAsNotSpam()->save();
When a partial submission is marked as spam, it stops reporting itself as partial β isPartial() returns false, so it can't be resumed, and it's skipped by the automatic partial submission cleanup. Under the hood, the internal _partial key is kept as a record that the submission was never finalized. That way, marking it as not spam and calling finalize() will send the submission through the full pipeline, as if it had just been submitted:
$submission->markAsNotSpam();
$submission->isPartial()
? $submission->finalize()
: $submission->save();
If a SubmissionCreating listener returns false while a spam submission is being finalized, finalize() returns false and the submission stays marked as spam.
Spam submissions are automatically deleted a configurable number of days after being marked as spam (30 by default).