英文:
Old Value/Search Value on search select button in Laravel
问题
以下是翻译好的内容:
在我的表格中,我创建了一个选择按钮,用于搜索特定数据。但是,当我点击按钮时,按钮不会保留或保持我之前点击的请求值,而是重置为选择全部。
这是我在 Blade 文件中的表单代码:
<form action="">
<select class="form-select" name="rumah_id" id="rumah_id">
<option value="">选择全部</option>
@foreach($rumahs as $rumah)
@if(old('rumah_id') == $rumah->id)
<option value="{{ $rumah->id }}">{{ $rumah->alamat }}</option>
@else
<option value="{{ $rumah->id }}">{{ $rumah->alamat }}</option>
@endif
@endforeach
</select>
</form>
这是我的控制器代码:
public function index(Request $request){
$rumah = Rumah::all();
$mobil = Mobil::with(['rumah'])->orderBy('rumah_id', 'asc')
->when($request->rumah_id != null, function($q) use($request){
return $q->where('rumah_id', 'like', '%'. $request->rumah_id. '%');
});
return view('mobil/index', [
'mobils' => $mobil,
'rumahs' => $rumah,
]);
}
这是我的 "rumah" 数据库迁移文件的样子:
Schema::create('rumahs', function (Blueprint $table) {
$table->id();
$table->string('alamat');
$table->timestamp('published_at')->nullable();
$table->timestamps();
});
请注意,我只翻译了代码部分,没有包含其他内容。
英文:
i make a select button for search specific data on my table.
but there is problem when i click the button, the button doesn't remain or stay from past request value that i click, or it reset back to select all
so this is my form in my blade file
<form action="">
<select class="form-select" name="rumah_id" id="rumah_id">
<option value="">Select All</option>
@foreach($rumahs as $rumah)
@if(old('rumah_id') == $rumah->id)
<option value="{{ $rumah->id }}">{{ $rumah->alamat }}</option>
@else
<option value="{{ $rumah->id }}">{{ $rumah->alamat }}</option>
@endif
@endforeach
</select>
</form>
this is my Controller
public function index(Request $request){
$rumah = Rumah::all();
$mobil = Mobil::with(['rumah'])->orderBy('rumah_id', 'asc')
->when($request->rumah_id != null, function($q) use($request){
return $q->where('rumah_id', 'like', '%'. $request->rumah_id. '%');
})
return view('mobil/index',[
'mobils' => $mobil,
'rumahs' => $rumah,
]);
}
also this is my migration database for rumah look like
Schema::create('rumahs', function (Blueprint $table) {
$table->id();
$table->string('alamat');
$table->timestamp('published_at')->nullable();
$table->timestamps();
});
答案1
得分: 0
尝试这个:
<select class="form-select" name="rumah_id" id="rumah_id">
<option value="">选择全部</option>
@foreach($rumahs as $rumah)
<option value="{{ $rumah->id }}" {{ old('rumah_id') == $rumah->id ? 'selected' : '' }}>{{ $rumah->alamat }}</option>
@endforeach
</select>
英文:
Try this :
<select class="form-select" name="rumah_id" id="rumah_id">
<option value="">Select All</option>
@foreach($rumahs as $rumah)
<option value="{{ $rumah->id }}" {{ old('rumah_id') == $rumah->id ? 'selected' : '' }}>{{ $rumah->alamat }}</option>
@endforeach
</select>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论