This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

KurtJensen
KurtJensen

How can I set the value of a field in new model object during creation in backend controller? I have a dropdown field and I want the default value to be set from a value in Settings when user creates a new record.

I tried :

public function beforeCreate()
{   
    $defaultPerm = Settings::get('permission', true);
    // set default vaule for permission drop down
    $this->permission_id = $defaultPerm;
}

in model but this did not work.

KurtJensen
KurtJensen

Part way there with this post. See: https://octobercms.com/forum/post/dynamic-default-values-for-form?page=1#post-3759

Only problem is that it overrides values that were actually set by users when the record is opened for editing. I only want for creating new records, not editing existing.

Last updated

lzodevelopment
lzodevelopment

I've done it something like this i know it's not the best solution but it gets the job done



        $currentSection  = SectionPost::where('post_id', '=', $this->id)->first();

        $section         = Section::find($currentSection->section_id);

        $sections        =  Section::lists("title", "id");

        $array =  array_merge([$currentSection->section_id => $section -> title], $sections);

        return array_unique($array);


Last updated

1-3 of 3

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.