Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 在Laravel中不使用按钮和ajax的自动筛选_Mysql_Laravel - Fatal编程技术网

Mysql 在Laravel中不使用按钮和ajax的自动筛选

Mysql 在Laravel中不使用按钮和ajax的自动筛选,mysql,laravel,Mysql,Laravel,我有三张桌子: Type: id, name Level: id, name Question: id, content, type_id, level_id 我用“过滤器”按钮完成了过滤器功能。但现在,我想要的是,当我过滤时,数据将自动加载到表中,而无需使用按钮。我该怎么办 这是我的代码使用按钮 过滤器控制器: public function filter(Request $request){ $types = Type::all(); $levels = L

我有三张桌子:

Type: id, name
Level: id, name
Question: id, content, type_id, level_id
我用“过滤器”按钮完成了过滤器功能。但现在,我想要的是,当我过滤时,数据将自动加载到表中,而无需使用按钮。我该怎么办

这是我的代码使用按钮

过滤器控制器:

public function filter(Request $request){
        $types = Type::all();
        $levels = Level::all();
        $model = Question::where('id', '>', 0);

        if (isset($request->type))
            $model = $model->where('type_id', $request->type);
        if (isset($request->level))
            $model = $model->where('level_id', $request->level);

        $data = $model->paginate(999)->appends(request()->query());

        return view('filter', compact( 'data', 'request', 'types','levels'));
    }
filter.blade.php

<form method="get">
    <select name="type">
        <option value="">All Types</option>
        @foreach ($types as $item)
            <option value="{{ $item->id }}" @if ($request->type == $item->id) selected @endif>{{ $item->name }}</option>
        @endforeach
    </select>
    <select name="level">
        <option value="">All Levels</option>
        @foreach ($levels as $item)
            <option value="{{ $item->id }}" @if ($request->level == $item->id) selected @endif>{{ $item->name }}</option>
        @endforeach
    </select>
    <button type="submit" class="btn btn-primary">Filter</button>
</form>
<table border="1">
    <tr>
        <th>ID</th>
        <th>Content</th>
    </tr>
    @foreach ($data as $q)
        <tr>
            <td>{{ $q->id }}</td>
            <td>{{ $q->content }}</td>
        </tr>
    @endforeach
</table>

所有类型
@foreach($type作为$item)
type==$item->id)选中@endif>{{{$item->name}
@endforeach
各级
@foreach($levels作为$item)
级别==$item->id)已选择@endif>{{{$item->name}
@endforeach
滤器
身份证件
所容纳之物
@foreach($q数据)
{{$q->id}
{{$q->content}
@endforeach

当选择值发生变化时,您只需提交表单即可:


...
或者,如果您不想使用表单,假设您的路径是
/questions
,并且您已在.env文件中设置了
APP\u URL
属性,则可以执行类似于以下操作:

<select name="type" onchange="window.location.href = this.value">
    <option value="{{ url('questions') }}">All types</option>
    @foreach($types as $type)
        <option value="{{ url('questions') }}?type={{ $type->id }}"{{ request('type') == $type->id ? ' selected' : '' }}>
            {{ $type->name }}
        </option>
    @endforeach
</select>
<option value="{{ url('questions') . '?' . (request()->filled('level') ? 'level=' . request('level') . '&' : '') . 'type=' . request('type') }}">
    {{ $type->name }}
</option>

所有类型
@foreach($type作为$type)
身份证?”所选“:”}}>
{{$type->name}
@endforeach
如果在按类型筛选时希望当前选定的级别保持选中状态,可以将选项值更改为以下值:

<select name="type" onchange="window.location.href = this.value">
    <option value="{{ url('questions') }}">All types</option>
    @foreach($types as $type)
        <option value="{{ url('questions') }}?type={{ $type->id }}"{{ request('type') == $type->id ? ' selected' : '' }}>
            {{ $type->name }}
        </option>
    @endforeach
</select>
<option value="{{ url('questions') . '?' . (request()->filled('level') ? 'level=' . request('level') . '&' : '') . 'type=' . request('type') }}">
    {{ $type->name }}
</option>

{{$type->name}

在我的手机上接听,因此可能会出现语法错误。

那么您应该使用ajax来完成此操作。Vue.js可以轻松满足您的需求。如果不使用ajax,还有其他功能吗?谢谢。我用了你的第一种方式。但url仍然在变化。我希望在筛选时url不会更改例如:url必须是abc.local/filter,而不是abc.local/filter?type=1&level=1您可以将表单的方法更改为
post
to,并显然更新路由文件以捕获请求并将其指向筛选方法。否则Ajax将是最好的选择。请在将表单的方法更改为post时显示详细代码好吗?