搜索过滤器laravel问题,从中获取值

时间:2018-09-15 08:14:17

标签: php laravel

我的控制器:

public function FilteredAdsShow(){

    $id= Input::get('category'); //get id from select name="categories"
    $tit = Input::get('titleFilter'); // get title from input text
    $categories = Category::all();

    $ads = Ad::where('category_id','=',$id)
        ->where('active','=',1)
        ->where('title','=',$tit)
        ->orderby('promoted','1')
        ->orderby('created_at','desc')
        ->with('images')
        ->get();

    return view('filtered')
        ->with(compact('ads'))
        ->with(compact('categories'));

}

我的视图上有一个按钮,单击该按钮即可调用该路由。 我也在做一个foreach来获取所有具有该标题的值,并且其中category_id = $ id,但它不起作用。

查看:

@extends('layouts.app')

@section('search')
    @include('pieces.search')
@endsection

    @foreach($ads as $ad)
        <div class="col-3 adSection">
            <img src="{{$ad->images->first()->path}}" alt="Imagem do anuncio" class="adImages"/>
            <p>{{$ad->title}}</p>
            <p>{{$ad->price}}</p>
        </div>
    @endforeach

我的搜索视图中包含我的搜索组件。

视图我称为搜索视图:

  

@section('搜索')       @include('pieces.search')@endsection

@foreach($ads as $ad)
    <div class="col-3 adSection">
        <img src="{{$ad->images->first()->path}}" alt="Imagem do anuncio" class="adImages"/>
        <p>{{$ad->title}}</p>
        <p>{{$ad->price}}</p>
    </div>
@endforeach

我的路线:

Route::get('/filteredAds','MainController@FilteredAdsShow')->name('filteredAds');

我的搜索视图:

<div class="col-6">
    <div class="form-group">
        <input type="text" name="titleFilter" placeholder="Pesquise pelo título" class="form-control">
    </div>
</div>
<div class="col-4">
    <div class="form-group">
        <select class="form-control" name="category">
            <option value="-1">Todos</option>
            @foreach($categories as $category )
                <option value="{{$category->id}}">{{$category->name}}</option>
            @endforeach
        </select>
    </div>

</div>
<div class="col-2">
    <div class="form-group">
        <a href="{{route('filteredAds')}}" class="btn btn-primary form-control">Pesquisar</a>
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

问题在您的 anchor标记中。您的锚标记只是在调用一条路由,但它没有向控制器发送任何值

要将选定的选项值发送到控制器,您将必须使用jquery。

  1. 获取标题值

HTML

具有某些ID的输入标签

    <input type="text" id="titleFilter" name="titleFilter" placeholder="Pesquise pelo título" class="form-control">

jQuery

    var titleFilter = $('#titleFilter').val();

2。获取选定的值

假设您的选择具有ID类别

<select class="form-control" name="category" id='categories'> ...</select>

$('#categories').on('change',function(){
   //get selected category
   var selectedOption = $('#categories:selected').val()  
   //trigger click here to your route
});

3.trigger在更改下拉列表或根据需要更改时单击按钮