dongluobei9359 2017-10-12 08:47
浏览 216
已采纳

如何在Laravel表单中提交表单后保留所选选项的值

I want to make when user search by country on the page refresh eg. result page to keep selected country on the option on the form.

This is what I have where I populate the dropdown

@foreach ($countries as $country)
        <option value="{!! $country->id !!}">{!! $country->name !!}</option>
@endforeach

And this is what I have tried but seems that is not correct since the value isn't selected

@foreach ($countries as $country)
    <option value="{!! $country->id !!}"  @if( old('country->id')  == $country->id) selected="selected" @endif>{!! $country->name !!}</option>
@endforeach

What's the trick here?

Here is the controller

public function search(Request $request)
{

    $searchText = strip_tags($request['q']);
    $seachLocation = strip_tags($request['l']);


    $columns =['alias','description','address1','address2','address3'];

    $query = Item::select('*');

    $query->where( 'title', 'like', '%'.$searchText.'%');

    foreach( $columns as $column) {
        $query->orWhere( $column, 'like', '%'.$searchText.'%', '%'.$seachLocation.'%');
    }

    $query->orWhereHas('category',function( $query ) use (  $searchText ) {
        $query->where('title', 'like', '%'.$searchText.'%' );
    });

    $query->orWhereHas('country',function( $query ) use (  $searchText ) {
        $query->where('name', 'like', '%'.$searchText.'%' );
    });

    $items = $query->paginate(5);
    $searchQuery = $searchText;
    $searchQueryLoc = $seachLocation;

    return view('search', compact('items','searchQuery','seachLocation'));
}
  • 写回答

3条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 vs创建SQL Server数据库时出现“系统找不到指定的路径”
      • ¥15 网页超时时间设置失效
      • ¥15 有关绿色信贷毕业论文的问题
      • ¥30 关于#机器人#的问题,如何解决?
      • ¥15 求MATLAB函数ScalarLayerDisplay的代码
      • ¥15 安卓如何自动执行检测到的NFC标签,无需再点确认
      • ¥15 pyHM库mouse模块的ValueError错误
      • ¥15 python opencv 摄像头 传感器
      • ¥30 eMMC&Android&C&Linux
      • ¥15 uniapp ios恢复购买