Record filter with Laravel 5.4. *

1

Good, I would like to know that a tool or method could be used for a dynamic record filter, for example, I have a file called contacts.blade.php where I show a kind of address book, what I want to do is place a mechanism (Input , Select) that allows me to filter the contacts faster as the user chooses, I leave exactly the frag of code that will show according to the filter:

<!-- Contacts -->
@foreach($Contacts as $Contact)
     <div class="col-md-4 col-sm-4 col-xs-12 profile_details">
          <div class="well profile_view">
               <div class="col-sm-12">
                    <h4 class="brief"
                         <i>{{ $Contact->positions }} | 
                            <small>{{ $Contact->depart }}</small></i>
                    </h4>
                     <div class="left col-xs-7">
                          <h2>{{ $Contact->f_name }} {{ $Contact->l_name }}</h2>
                                            <ul class="list-unstyled">
                                                <li><i class="fa fa-envelope-o"></i> {{ $Contact->email }}</li>
                                                <li><i class="fa fa-phone"></i> 40-215</li>
                                                <li><i class="fa fa-calendar"></i> {{ Carbon\Carbon::parse($Contact->date_bjob)->diffForHumans() }}</li>
                                            </ul>
                                        </div>
                                        <div class="right col-xs-5 text-center">
                                            <img src="{{ Gravatar::src($Contact->email) }}" alt="" class="img-circle img-responsive">
                                        </div>
                                    </div>
                                    <div class="col-xs-12 bottom text-center">
                                        <div class="col-xs-12 col-sm-6 emphasis">
                                        </div>
                                        <div class="col-xs-12 col-sm-6 emphasis">
                                            <button type="button" class="btn btn-success btn-xs"> <i class="fa fa-user">
                                            </i> <i class="fa fa-comments-o"></i> </button>
                                            <a href="{{ route('profile', $Contact->username ) }}" class="btn btn-primary btn-xs">
                                                <i class="fa fa-user"> </i> Ver Perfil
                                            </a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            @endforeach
                            <!-- ./Contacts -->

The Controller

public function index()
{
    Carbon::setLocale('es');
    $Contacts = dk_profile_user::
                select('dk_profile_user.id','dk_profile_user.f_name','dk_profile_user.l_name','users.email','dk_profile_user.address',
                    'dk_profile_user.status','dk_profile_user.image_profile','dk_profile_user.date_bday',
                    'dk_profile_user.date_bjob','dk_master_area.name as depart','dk_area.name as area','dk_profession.name as profession',
                    'dk_positions.name as positions','dk_profile_user.username')
                ->join('dk_area','dk_area.id','=','dk_profile_user.id_area')
                ->join('dk_master_area','dk_master_area.id','=','dk_area.id_master_area')
                ->join('dk_profession','dk_profession.id','=','dk_profile_user.id_profession')
                ->join('dk_positions','dk_positions.id','=','dk_profile_user.id_positions')
                ->join('users','users.id','=','dk_profile_user.id')
                ->get();
    return View('/contacts')->with('Contacts',$Contacts);
}

UPDATE:

According to the response of @Pablo Contreras, try to adapt it to my project, but it returns an error in the console: POST http://192.168.21.210:8000/contacts/m/0 500 (Internal Server Error)

My Route:

// CONTACTOS
Route::get('contacts','contacts\[email protected]');
Route::get('listall/{page?}','contacts\[email protected]');
Route::post('contacts/{field1}/{field2}','[email protected]');

My Controller

public function listadoContactos(Request $req, $field1, $field2)
{
    if($req->ajax()) {
        $result = dk_profile_user::
                    select('dk_profile_user.id',DB::raw('CONCAT(dk_profile_user.f_name, " ", dk_profile_user.l_name) AS name'),
                        'users.email','dk_profile_user.address','dk_profile_user.status','dk_profile_user.image_profile',
                        'dk_profile_user.date_bday', 'dk_profile_user.date_bjob','dk_master_area.name as depart',
                        'dk_area.name as area','dk_profession.name as profession','dk_positions.name as positions',
                        'dk_profile_user.username')
                    ->join('dk_area','dk_area.id','=','dk_profile_user.id_area')
                    ->join('dk_master_area','dk_master_area.id','=','dk_area.id_master_area')
                    ->join('dk_profession','dk_profession.id','=','dk_profile_user.id_profession')
                    ->join('dk_positions','dk_positions.id','=','dk_profile_user.id_positions')
                    ->join('users','users.id','=','dk_profile_user.id');
                    if($field1!=="") {
                        $result->where('dk_profile_user.name','like','%'.$field1.'%');
                    }
                    if ($field2!==0) {
                        $result->where('dk_profile_user.area','=',$field2);
                    }
                    $results = $result->get();
        return Response::json(array('resultado' => $results));
    }
}

Script in my view

//Para un input type text:
        $('#person').keyup(function(e) {
            Contactos();
        });
        //Para un select:
        $('#departament').on( 'change', function (e) {
            Contactos();
        });
   function Contactos() {
            var field1 = $("#person").val();
            var field2 = $("#departament").val();
            $.post("contacts/"+field1+"/"+field2+"" ,function(response){
                console.log(response.resultado); 
            });
        };
    
asked by Maykol Rivas 08.03.2017 в 19:37
source

2 answers

2

I suppose you use jQuery, and if not, here is the cdn link

1. On the route:

Route::post('listadoContactos/{field1}/{field2}','[email protected]');

2. On the controller:

public function getListadoContactos(Request $req, $field1, $field2){
    if($req->ajax()){
        $result = dk_profile_user::
                select('dk_profile_user.id','dk_profile_user.f_name','dk_profile_user.l_name','users.email','dk_profile_user.address',
                    'dk_profile_user.status','dk_profile_user.image_profile','dk_profile_user.date_bday',
                    'dk_profile_user.date_bjob','dk_master_area.name as depart','dk_area.name as area','dk_profession.name as profession',
                    'dk_positions.name as positions','dk_profile_user.username')
                ->join('dk_area','dk_area.id','=','dk_profile_user.id_area')
                ->join('dk_master_area','dk_master_area.id','=','dk_area.id_master_area')
                ->join('dk_profession','dk_profession.id','=','dk_profile_user.id_profession')
                ->join('dk_positions','dk_positions.id','=','dk_profile_user.id_positions')
                ->join('users','users.id','=','dk_profile_user.id')
                ->where('users.id','=',$field1)
                ->where('users.id','=',$field2)
                ->get();
        return Response::json(array('resultado' => $result));
    }
}

3. In the view, in the script , you will place the function and its call in ready (so that it executes when you load the route).

$(document).ready(function () {
    Contactos();
});

function Contactos() {
    var field1 = $("#field1").val();
    var field2 = $("#field2").val();
    //obtendras los valores de los distintos campos y los enviaras por parametros al controlador.
    //puedes inventarte un array que contenga todos los valores de los campos de la vista y así los envías en un solo parámetro al controlador. No tengo chance para probar así que lo dejo a tu propia experiencia :D
    $.post("listadoContactos/"+field1+"/"+field2+"" ,function(response){
        console.log(response.resultado); //response contiene todo lo que obtienes de la funcion en el controlador
    });
};

4. To be executed in each state change of each field you should only use its different forms:


//Para un input type text:
$('#field1').keyup(function(e) {
    Contactos();
});

//Para un select:
$('#field2').on( 'change', function (e) {
    Contactos();
});

NOTE: Do not send the variable to the view when you do it with the function of the index, in this way that I am showing you loads the results of the list of the controller with the filtering that you manage. p>

PD: With nothing more to narrow, this would be the way to create the list of contacts how you handle it and how to filter it.

Greetings, we are here to help each other: D

    
answered by 08.03.2017 / 19:45
source
1

@PabloContreras Thanks for your response, I was able to find a solution by changing a couple of things so that they could work for me.

1. On the route:

Before:

Route::post('listadoContactos/{field1}/{field2}','[email protected]');

After:

Route::get('contacts/{field1}/{field2}','contacts\[email protected]');

2. On the controller:

Before:

public function getListadoContactos(Request $req, $field1, $field2){
 if($req->ajax()){
    $result = dk_profile_user::
            select('dk_profile_user.id','dk_profile_user.f_name','dk_profile_user.l_name','users.email','dk_profile_user.address',
                'dk_profile_user.status','dk_profile_user.image_profile','dk_profile_user.date_bday',
                'dk_profile_user.date_bjob','dk_master_area.name as depart','dk_area.name as area','dk_profession.name as profession',
                'dk_positions.name as positions','dk_profile_user.username')
            ->join('dk_area','dk_area.id','=','dk_profile_user.id_area')
            ->join('dk_master_area','dk_master_area.id','=','dk_area.id_master_area')
            ->join('dk_profession','dk_profession.id','=','dk_profile_user.id_profession')
            ->join('dk_positions','dk_positions.id','=','dk_profile_user.id_positions')
            ->join('users','users.id','=','dk_profile_user.id')
            ->where('users.id','=',$field1)
            ->where('users.id','=',$field2)
            ->get();
    return Response::json(array('resultado' => $result));
 }
}

After:

public function listadoContactos(Request $req, $field1, $field2)
{
    if($req->ajax()) {
        $result = dk_profile_user::
                    select('dk_profile_user.id','dk_profile_user.f_name', 'dk_profile_user.l_name',
                        'users.email','dk_profile_user.address','dk_profile_user.status','dk_profile_user.image_profile',
                        'dk_profile_user.date_bday', 'dk_profile_user.date_bjob','dk_master_area.name as depart',
                        'dk_area.name as area','dk_profession.name as profession','dk_positions.name as positions',
                        'dk_profile_user.username')
                    ->join('dk_area','dk_area.id','=','dk_profile_user.id_area')
                    ->join('dk_master_area','dk_master_area.id','=','dk_area.id_master_area')
                    ->join('dk_profession','dk_profession.id','=','dk_profile_user.id_profession')
                    ->join('dk_positions','dk_positions.id','=','dk_profile_user.id_positions')
                    ->join('users','users.id','=','dk_profile_user.id');
                    if($field1!=="") {
                        $result->where('dk_profile_user.f_name','like','%'.$field1.'%');
                        $result->orWhere('dk_profile_user.l_name','like','%'.$field1.'%');
                    }
                    if ($field2!=='0') {
                        $result->where('dk_profile_user.area','=',$field2);
                    }
                    $results = $result->get();
        return \Response::json(array('resultado' => $results));
    }
}

3. In the view, in the script.

Before:

function Contactos() {
     var field1 = $("#field1").val();
     var field2 = $("#field2").val();
    $.post("listadoContactos/"+field1+"/"+field2+"" ,function(response){
    console.log(response.resultado); 
     });
};

After:

function Contactos() {
            var field1 = $("#person").val();
            var field2 = $("#departament").val();
            $.ajax({
                type: 'GET',
                url: 'contacts/' + field1 +'/'+ field2,
                success: function(response){
                    console.log(response.resultado); 
                }
            });
        }

4. To be executed in each change of state of each field you should only use its different forms:

This is the same:

//Para un input type text:
$('#field1').keyup(function(e) {
    Contactos();
});
//Para un select:
$('#field2').on( 'change', function (e) {
    Contactos();
});

Thank you very much, although some small changes were made, the logic of the answer helps me a lot to find a solution that works correctly in my project.

    
answered by 10.03.2017 в 13:21