Laravel 5.2通过多个参数进行AJAX过滤

时间:2016-10-02 05:24:09

标签: laravel laravel-5.2

我正在尝试为我的汽车目录制作AJAX过滤器,制作逻辑:“如果我在请求中有SALES_NAME,我会过滤它(添加到数据库中的过滤器)”,但我不明白该怎么做

我有参数 - 制造商,型号,所有者。我想过滤任何一辆车。如果我只选择一个,例如所有者 - 它将返回所有者的所有车辆,如果我选择丰田 - 所有的toyotas,或者所有车主的名称Bob的丰田汽车。以下是我的观点的一部分:

{!! Form::open(['id'=>'filter']) !!}
{{csrf_field()}}
<div class="form-group col-sm-2">
    {!! Form::select('sales_id', ['' => 'Owner'] + $sales, null, ['class'=>'form-control', 'id'=>'sales_id']) !!}
</div>
<div class="form-group col-sm-2">
    {!! Form::select('manufacturer_id', ['' => 'Manufacturer'] + $manufacturers, null, ['class'=>'form-control', 'id'=>'manufacturer_id']) !!}
</div>
<div class="form-group col-sm-1">
    <button type="button" class="btn btn-start" data-toggle="modal" data-target="#addmanufacturer  ">
        <i class="fa fa-plus" aria-hidden="true"></i>
    </button>
</div>
<div class="form-group col-sm-2">
    {!! Form::select('model_id', ['' => 'Model'], null, ['class'=>'form-control', 'id'=>'model_id']) !!}
</div>
<div class="form-group col-sm-1">
    <button type="button" class="btn btn-start" data-toggle="modal" data-target="#addmodel">
        <i class="fa fa-plus" aria-hidden="true"></i>
    </button>
</div>
<button type="submit" class="btn"><i class="fa fa-search" aria-hidden="true"></i></button>
{!! Form::close() !!}

JS:

 $('#manufacturer_id').on('change', function(e){
        var man_id = e.target.value;
        $.get('/admin/getmodels/models?man_id=' + man_id, function(data){
            $('#model_id').empty();
            $.each(data, function(index, model){
                $('#model_id').append('<option value="' + model.id+'">'
                        + model.name + ' ' + model.year + '</option>');
            });
        });
    });

    $('#filter').submit(function(event) {
        event.preventDefault();
        $('#cars').empty();
        $.post('/admin/getmodels/carfilter', $('#filter').serialize(), function (cars) {
                $.each(cars, function (index, car) {
                    $.post('/admin/getmodels/car', {car_id: car.id}, function (car) {
                        $('#cars').append('<div class="col-sm-2" id="car'+car.id+'"></div>');
                        if (car.photos.length > 0) {
                            $.each(car.photos, function (index, photo) {
                                if (index == 0) {
                                    $('#car' + car.id).append('<img class="img img-responsive" src="' + photo.file + '">').append('<div class="header header4 text-center">'+car.manufacturer.name+' '+car.model.name+' '+car.model.year+'</div>');
                                }
                            });
                        } else {
                            $.each(car.modelphoto, function (index, photo) {
                                if (index == 0) {
                                    $('#car' + car.id).append('<img class="img img-responsive" src="' + photo.file + '">').append('<div class="header header4 text-center">'+car.manufacturer.name+' '+car.model.name+' '+car.model.year+'</div>');
                                }
                            });
                        }
                    });
                });
            });
        });

控制器,我肯定不正确,但看起来我想做:

 public function postCarfilter(Request $request)
{
    $cars = Car::where('manufacturer_id', $request->manufacturer_id)->where('model_id',$request->model_id)->get();
    if ($request->sales_id != ""){
        $cars = $cars->where('user_id', $request->sales_id)->get();
    }
    return Response::json($cars);
}

型号:

class Car extends Model
{
public function user (){
    return $this->belongsTo('App\User');
}

public function manufacturer (){
    return $this->belongsTo('App\Manufacturer');
}

public function model(){
    return $this->belongsTo('App\CarModel');
}

public function photos(){
    return $this->morphMany('App\Photo', 'imageable');
}

}

class User extends Authenticatable
{
public function car() {
    return $this->hasMany('App\Car');
}
}

class Manufacturer extends Model
{

public function car() {
    return $this->hasMany('App\Car');
}

public function model() {
    return $this->hasMany('App\CarModel');
}
}

class CarModel extends Model
{

public function car() {
    return $this->hasMany('App\Car');
}

public function manufacturer(){
    return $this->belongsTo('App\Manufacturer');
}
}

1 个答案:

答案 0 :(得分:0)

好的,我明白了,我必须使用查询构建器,所以新控制器:

pandas.dataFrame.sort_values()

也许这对某些人有用