Tony S
Tony S

Reputation: 571

Laravel model creation fields dynamic

Here my code :

            $context = new Context;
            $context->name = 'blabla';
            if($original_lang === 'en') {
                $context->en = $sentence;
            } else if($original_lang === 'fr') {
                $context->fr = $sentence;
            } else if($original_lang === 'ja') {
                $context->ja = $sentence;
            }
            $context->save;

I think the part with original lang is redundant, anyone know a best way to write this ? thanks !

Upvotes: 0

Views: 31

Answers (1)

John Lobo
John Lobo

Reputation: 15319

You can write as below

 $context->{$original_lang} = $sentence;

Upvotes: 1

Related Questions