Scope ajax does not work laravel 5.4

0

It turns out that the query scope does not work, I'm using ajax to capture the data, the following happens to me when in the controller I load the pager, the table shows me the records

$dataInsumo = Insumo::orderBy('descripcionIns','DESC')->paginate(5);

But when I load the scope Buscar , it does not show any

$dataInsumo = Insumo::Buscar($request->scope)->orderBy('descripcionIns','DESC')->paginate(5);

I enclose my InsumoController

public function index(Request $request)
{   
    $dataInsumo = Insumo::Buscar($request->scope)->orderBy('descripcionIns','DESC')->paginate(5);
    if ($request->ajax()) {
        return response()->json(view('insumo.table',compact('dataInsumo'))->render());
    }
    //TUTORIAL https://www.youtube.com/watch?v=2D7CVpUh8vQ
    $titulo1 = 'Mantenimiento de Insumos';
    $tipo = TipoInsumo::all();
    return view('insumo.index')
    ->with(['dataInsumo'=>$dataInsumo])
    ->with('titulo1',$titulo1)
    ->with('tipo',$tipo);
}

I enclose my model Insumo

public function scopeBuscar($query, $scope=''){
    return $query->where('descripcionIns','LIKE',"%scope%");  
}

I enclose my Insumo.js This code is for ajax paging

$(document).on('click','.pagination a',function(e){
e.preventDefault(); 
var page = $(this).attr('href').split('page=')[1];
var route = 'http://localhost/mshalom/public/insumo';

$.ajax({
    url:route,
    data: {page: page},
    type: 'GET',
    dataType: 'json',
    success: function(result){
        $("#divtable").html(result);
    }
});
});

This code captures the value of the text #scope, probe with alert and captures the text

$(document).on('click','#btnbuscar',function(e){
var route = 'http://localhost/mshalom/public/insumo';
var scope = $("#scope").val();
$.ajax({
    url:route,
    data: {scope: scope},
    type: 'GET',
    dataType: 'json',
    success: function(result){
        $("#divtable").html(result);
    }
});
});

I enclose my View Insumo.index , in slot insumo.table I only charge the table with records.

<div class="row">
<div class="col-sm-6"> <!-- Boton Nuevo -->
    <a class="btn btn-default" href="{{ url('insumo/create') }}"><span class="glyphicon glyphicon-file"></span> Nuevo Insumo</a>
</div>
<div class="col-sm-3 form-horizontal">

</div>
<div class="col-sm-3 ">  <!-- Caja de Texto de busqueda -->
    <div class="input-group">
        <!-- Caja de Texto de busqueda -->
        <input id="scope"  type="text" class="form-control" placeholder="Digite insumo...">
        <span class="input-group-btn">
        <!-- Boton -->
        <button id = "btnbuscar" class="btn btn-primary">Buscar</button>
        </span>
</div>
</div>
<div class="col-sm-12 " id="divtable">
@component('insumo.table')
    @slot('dataInsumo',$dataInsumo)
@endcomponent
</div>      
</div>            
    @endsection

   @section('js')
  <script src="{{asset('js/mshalomjs/insumo.js')}}"></script>
  @endsection

I am very grateful for your response beforehand.

    
asked by Daviss9 12.07.2017 в 03:46
source

1 answer

0

Thank you, for your help. I found the error, it's in the model

I had to declare the scope variable concatenated with%

public function scopeBuscar($query, $scope=''){
return $query->where('descripcionIns','LIKE',"%".$scope."%");  
}

I was declaring it "% $ scope%" so I was not looking for any record.

    
answered by 15.07.2017 / 02:20
source