10 months ago

How To Submit Form If The User Don't Want To Add hasMany FormItems

Posted 10 months ago by partabsaifzakir

I'm using Laravel 5.7.*.

I have form which hasMany formItems, Like a form hasMany formItems & formItems belongsTo form, but i want an if between them, that if user don't want to add formItems only form data store in DB and if user want to add formItems both data store in DB, right now, it's storing both data in DB, but i don't how to make it store if user don't want formItems.

Here is my FormController store method():

    public function store(Request $request)
        $form = Form::create([
            'user_phone' => $request['user_phone'],
            'user_name' => $request['user_name'],
        $form_items = [];
        foreach($request['formItems'] as $form_item) {
            $form_items[] = new FormItem([
                'form_id' => $form->id,
                'family_name' => $form_item ['family_name'],
                'family_phone' => $form_item ['family_phone'],

Image For Better Understanding: enter image description here

enter image description here

Please sign in or create an account to participate in this conversation.