Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

php - How can I fetch record drom database in edit view In Laravel?

In my edit view I have code like this

<div class="form-group">
    <label class="col-md-12">Last Name</label>
    <div class="col-md-12">
        <input type="text" placeholder="Enter Last Name" name="lastName" class="form-control form-control-line" value="{{$profile->personal_detail['last_name']}}" required>
    </div>
</div>

<div class="form-group">
    <label class="col-md-12">Department</label>
    <div class="col-md-12">
        <select class="custom-select form-control col-md-11" id="department" name="department">{{ $profile->personal_profile['department'] }}
            @foreach($listDepartment as $departmentList){
                <option value='{{$departmentList->nameOfDepartment}}'>{{$departmentList->nameOfDepartment}}</option>
            }
            @endforeach
        </select>
    </div>
</div>

In edit view my last name field it gives me last name from database, in department it display me drop down of department but I want that inserted name of department in that field.

how can i get it??

I have other drop down like this

<div class="row">
<label class="col-md-6"><b> Mode </b></label>
<div class="col-md-6">
<select class="custom-select form-control col-md-12" name="mode" id="mode" required>
   <option value=""> --- Select Interciew Mode --- </option>
   <option value="telephonic">Telephonic</option>
   <option value="facetoface">Face 2 face</option>
   <option value="skype">Skype</option>
</select>
</div>
</div><hr>

This is my controller

public function candidateDetail($id)
    {
        $empDetails = User::all();
        $candidateDetail = EmployeeHire::find($id);

        $interview = [
            '' => '--- Select Interciew Mode ---',
            'telephonic' => 'Telephonic',
            'facetoface' => 'Face 2 face',
            'skype' => 'Skype'
        ];

        return view('pages.candidatedetails', compact('id', 'candidateDetail', 'empDetails', 'interview'));
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can check in your foreach if the value of nameOfDepartment match the one from your user.

<div class="form-group">
    <label class="col-md-12">Department</label>
    <div class="col-md-12">
        <select class="custom-select form-control col-md-11" id="department" name="department">
            @foreach($listDepartment as $departmentList)
                @if ($profile->personal_profile['department'] == $departmentList->nameOfDepartment)
                    <option value="{{$departmentList->nameOfDepartment}}" selected="selected">{{$departmentList->nameOfDepartment}}</option>
                @else
                    <option value="{{$departmentList->nameOfDepartment}}">{{$departmentList->nameOfDepartment}}</option>
                @endif
            @endforeach
        </select>
    </div>
</div>

For your second select field, create an array with all possible values in your controller.

$interview = [
    '' => '--- Select Interciew Mode ---',
    'telephonic' => 'Telephonic',
    'facetoface' => 'Face 2 face',
    'skype' => 'Skype'
];

Then you can do the same as your previous select :

<select class="custom-select form-control col-md-12" name="mode" id="mode" required>
    @foreach($interview as $key => $name)
        @if ($profile->personal_profile['interview'] == $key)
            <option value="{{ $key }}" selected="selected">{{ $name }}</option>
        @else
            <option value="{{ $key }}">{{ $name }}</option>
        @endif
   @endforeach
</select>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...