vendredi 24 juin 2016

laravel 5 search multiple fields

I have a php code in multiple search field but i dont know How i can use this function in laravel

the mysql syntax in laravel it's not the same in simple php so how i can compile it.

if(isset($_GET['submit'])) {
// define the list of fields
      if(isset($_GET['debit']) && !empty($_GET['debit'])) {

    $_GET['valeur'] = - $_GET['debit'];
  }
  else
  {
    if(isset($_GET['credit']) && !empty($_GET['credit']))
    {
        $_GET['valeur'] = $_GET['credit'];
    }
  }
    $fields = array('id_type', 'date_operation', 'date_valeur', 'numero','tiers','description','valeur');
    $conditions = array();


// builds the query
$query = "SELECT * FROM ecritures WHERE id_compte ='" . $iduser . "' ";


// loop through the defined fields
foreach($fields as $field){
    // if the field is set and not empty
    if(isset($_GET[$field]) && !empty($_GET[$field])) {
        // create a new condition while escaping the value inputed by the user (SQL Injection)
        $conditions[] = "$field LIKE '%" . mysqli_real_escape_string($co, $_GET[$field]) . "%'";
        }
    }


// if there are conditions defined
if(count($conditions) > 0) {
    // append the conditions
    $query .= " AND " . implode (' AND ', $conditions); // you can change to 'OR', but I suggest to apply the filters cumulative
}}

Aucun commentaire:

Enregistrer un commentaire