doz59484 2018-09-15 08:14
浏览 60

使用获取值搜索过滤器laravel问题

My Controller:

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'));

}

I have a button on my view that calls a route when it is clicked. I am doing a foreach too to get all values that has that title and where category_id = $id but it's not working.

View:

@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

My search components are on my search view i'm including.

View I call my search view:

@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

And my route:

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

My search view:

<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条回答 默认 最新

  • dran0703 2018-09-15 08:20
    关注

    Problem is in your anchor tag. your anchor tag is just calling a route but it is not sending any value to controller

    To send selected option value to controller you will have to use jquery.

    1. get title value

    HTML:

    Input tag having some id

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

    Jquery

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

    2. get selected value

    suppose your select has id categories

    <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 click on button when you change dropdown or however you want

    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)