在Laravel中搜索选择按钮上的旧数值/搜索数值

huangapple go评论52阅读模式
英文:

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

&lt;form action=&quot;&quot;&gt;
				&lt;select class=&quot;form-select&quot; name=&quot;rumah_id&quot; id=&quot;rumah_id&quot;&gt;
					&lt;option value=&quot;&quot;&gt;Select All&lt;/option&gt;
					
					@foreach($rumahs as $rumah)
						@if(old(&#39;rumah_id&#39;) == $rumah-&gt;id)
							&lt;option value=&quot;{{ $rumah-&gt;id }}&quot;&gt;{{ $rumah-&gt;alamat }}&lt;/option&gt;
						@else
							&lt;option value=&quot;{{ $rumah-&gt;id }}&quot;&gt;{{ $rumah-&gt;alamat }}&lt;/option&gt;
						@endif

					@endforeach
				&lt;/select&gt;
&lt;/form&gt;

this is my Controller

public function index(Request $request){
        $rumah = Rumah::all();
        $mobil = Mobil::with([&#39;rumah&#39;])-&gt;orderBy(&#39;rumah_id&#39;, &#39;asc&#39;)
        -&gt;when($request-&gt;rumah_id != null, function($q) use($request){
            return $q-&gt;where(&#39;rumah_id&#39;, &#39;like&#39;, &#39;%&#39;. $request-&gt;rumah_id. &#39;%&#39;);
        })
       return view(&#39;mobil/index&#39;,[
            &#39;mobils&#39; =&gt; $mobil,
            &#39;rumahs&#39; =&gt; $rumah,
        ]);
    }

also this is my migration database for rumah look like

Schema::create(&#39;rumahs&#39;, function (Blueprint $table) {
 $table-&gt;id();
 $table-&gt;string(&#39;alamat&#39;);
 $table-&gt;timestamp(&#39;published_at&#39;)-&gt;nullable();
 $table-&gt;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 :

            &lt;select class=&quot;form-select&quot; name=&quot;rumah_id&quot; id=&quot;rumah_id&quot;&gt;
                &lt;option value=&quot;&quot;&gt;Select All&lt;/option&gt;
                @foreach($rumahs as $rumah)
                    &lt;option value=&quot;{{ $rumah-&gt;id }}&quot; {{ old(&#39;rumah_id&#39;) == $rumah-&gt;id ? &#39;selected&#39; : &#39;&#39; }}&gt;{{ $rumah-&gt;alamat }}&lt;/option&gt;
                @endforeach
            &lt;/select&gt;

huangapple
  • 本文由 发表于 2023年2月10日 12:45:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407047.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定