Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pedroroccon's avatar

Auto selecting the <select> tag based on database informations

Hello guys! Thanks for the interest!

I have a question. My database has a field called "status" setted to type "char". In my model was defined an Assessor for the "status" field:

public function getStatusAttribute($value)
{
    switch($value):
        case 'APR': return 'Approved'; break;
        case 'DND': return 'Denied'; break;
        case 'PND': return 'Pending'; break;
        default: return 'Not Defined'; break;
    endswitch;
}

I have an edit form developed in Blade Template Engine. Here is my select field:

<div class="row">
    <div class="col-md-12 form-group">
        {!! Form::label('status', 'Status', ['class' => 'control-label']) !!}
        {!! Form::select('status', [
            '' => 'Please, select', 
            'APR' => 'Approved', 
            'DND' => 'Denied', 
            'PND' => 'Pending', 
        ], null, ['class' => 'form-control', 'required' => 'required']) !!}
    </div>
</div>

The problem is: When receiving a register from my database, the isn't setting the attribute "selected" to the current register that I call. For example, let's assume that I have a register with "status" setted to "DND". When I enter in my edit form, I want to my application automatically sets the to "Denied". I'm doing something wrong?

Thank you very much!

0 likes
15 replies
Mariam's avatar

@pedroroccon try this

{!! Form::select('status', [
            '' => 'Please, select', 
            'APR' => 'Approved', 
            'DND' => 'Denied', 
            'PND' => 'Pending', 
        ], Input::old('status'), ['class' => 'form-control', 'required' => 'required']) !!}
1 like
pedroroccon's avatar

@Mariam Everytime that I call $model->status It changes to "Approved" automaticly, because of my Assessor. I've tried add "Input::old('status')", but no success :(.

My forms to create and update the model are the same!

Mariam's avatar

@pedroroccon why are you using this?

public function getStatusAttribute($value)
{
    switch($value):
        case 'APR': return 'Approved'; break;
        case 'DND': return 'Denied'; break;
        case 'PND': return 'Pending'; break;
        default: return 'Not Defined'; break;
    endswitch;
}
Mo7sin's avatar

Input::old('status') is not going to work since it will get old value from the previous request, And we're talking about database here not form request.

So you can try this

{!! Form::select('status', [
            '' => 'Please, select', 
            'APR' => 'Approved', 
            'DND' => 'Denied', 
            'PND' => 'Pending', 
        ], $model->status, ['class' => 'form-control', 'required' => 'required']) !!}
pedroroccon's avatar

@Mariam I'm using this on my Model.php. I use because every time that i call $model->status the application changes my output to "Approved", instead of APR. This way my users can understand the status.

Mariam's avatar

@pedroroccon yeah,but you hardcoded here

 '' => 'Please, select', 
 'APR' => 'Approved', 
  'DND' => 'Denied', 
 'PND' => 'Pending', 
pedroroccon's avatar

@Mo7sin It still not working. But anyway, if I choose to this method, when I create a new item, my app will crash, because I use the same form.blade to create and edit my registers.

Mariam's avatar

@pedroroccon or u can do smth like this

 [
            '' => 'Please, select', 
            'Approved' => 'Approved', 
            'Denied' => 'Denied', 
            'Pending' => 'Pending', 
        ],

and then insert or update => "APR" instead of "Approved" in your controller

pedroroccon's avatar

@Mariam Do you have another solution? Maybe, something more easy. I'm rewriting the attributes in my because I need to use this form for both: create and update. So, when the user is creating a new register, it needs to know the available attributes.

Mo7sin's avatar

You can do this, so you can use form in both actions.

{!! Form::select('status', [
            '' => 'Please, select', 
            'APR' => 'Approved', 
            'DND' => 'Denied', 
            'PND' => 'Pending', 
        ], $model->status ?: null , ['class' => 'form-control', 'required' => 'required']) !!}
1 like
Mariam's avatar

@pedroroccon try to get smth like this

get(['status','status as selected'])

and then

{!! Form::select('status', [
            '' => 'Please, select', 
            'APR' => 'Approved', 
            'DND' => 'Denied', 
            'PND' => 'Pending', 
        ], isset($model) ? $model->selected : null , ['class' => 'form-control', 'required' => 'required']) !!}
1 like
pedroroccon's avatar
pedroroccon
OP
Best Answer
Level 10

@Mo7sin @Mariam Thank you very much for the help!

I solved my problem doing this:

{!! Form::select('status', [
            '' => 'Please, select', 
            'APR' => 'Approved', 
            'DND' => 'Denied', 
            'PND' => 'Pending', 
        ], $model['original']['status'] ?: null , ['class' => 'form-control', 'required' => 'required']) !!}

Thank you @Mo7sin and @Mariam for the tips!

1 like

Please or to participate in this conversation.