Nosean
136
2
Laravel

Dynamic Var Name by Store method

Posted 2 months ago by Nosean

How can I give a variable a dynamic name? Have 5 form fields to be entered in a DB. The name of the fields I have assigned value1 to value5.

My Store Method:

 public function store(Request $request)
    {
        switch ($request->formElement) {
            case 'input':
            case 'checkbox':
            case 'textarea':
                // Anlegen eines Extras Anhand der undercategorie ID und der FormElement Input, Checkbox, Textarea
                $extra = new UnderCategorieExtra;
                $extra->under_categorie_id = $request->undercategorie_id;
                $extra->extra_name_de = $request->fieldname_de;
                $extra->extra_name_en = $request->fieldname_en;
                $extra->extra_form_element = $request->formElement;
                $extra->placeholder_de = $request->placeholder_de;
                $extra->placeholder_en = $request->placeholder_en;
                $extra->save();
                return back();
                break;
            case 'select':
            case 'radio':
                // Anlegen des Extras Anhand der undercategorie_id und der Formelemente Select, Radio
                $extra = new UnderCategorieExtra;
                $extra->under_categorie_id = $request->undercategorie_id;
                $extra->extra_name_de = $request->fieldname_de;
                $extra->extra_name_en = $request->fieldname_en;
                $extra->extra_form_element = $request->formElement;
                $extra->placeholder_de = $request->placeholder_de;
                $extra->placeholder_en = $request->placeholder_en;
                $extra->save();

                // Values werden gespeichert mit ID
                for($i=0; $i<5; $i++) {
                    $extra_value = new ExtraValue;
                    $extra_value->under_categorie_extras_id = $extra->id;
                    $extra_value->extra_form_element = $request->formElement;
                    $extra_value->extra_form_value = $request->value1;
                    $extra_value->save();
                };

            return back();

        }
    }

Now how can I write the name dynamically so that the store method recognizes the 5 different fields and stores the entries

Did it with `$ request-value. $ I` tried without success

Thanks

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