SoFunction
Updated on 2025-03-02

Laravel framework executes native SQL statements and uses paginate paging methods

This article describes the method of executing native SQL statements and using paginate paging in Laravel framework. Share it for your reference, as follows:

1. Run native sql

public function getList($data){
//Get the parameters transmitted from the front end  $user = $data['userId'];
  $office = $data['officeId'];
  $key = $data['oneKeySearch'];
//Carry fuzzy search and joint query  $where = 'and 1=1 ';
  if($key!=null) {
    $where.= ' and (  like "%' . $key . '%"';
    $where.= ' or  like "%' . $key . '%"';
    $where.= ' or  like "%' . $key . '%")';
  }
//Judge the fields sent back by the front end, and if they are not empty, execute the conditional query  if($user!=null){
    $user='and ='.$user;
  }
  if($office!=null){
    $office='and ='.$office;
  }
//Customize native sql statements, %s can pass parameters into sql statements, the format is as follows:  $sqlTmp=sprintf('select ,,,,
            b.`realName` as userName,c.`name` as officeName
            from xxxa1
            LEFT JOIN xxx2 b ON =
            LEFT JOIN xxx3 c ON =
    where a.deleted_at is null and 1=1 %s %s %s ORDER BY 
    ', $where,$office,$user);
//Execute SQL statement  $results = DB::select($sqlTmp);
//Return result  return $results;
}

2. Run the query builder

public function getList($data){
//Get the parameters transmitted from the front end  $user = $data['userId'];
  $office = $data['officeId'];
  $key = $data['oneKeySearch'];
/*
  * 1. Table alias: directly "table name as table1" (below is xxx1 as a)
  * 2. Left connection: DB::table('Table 1')
  * ->leftJoin('Table 2', 'Table', '=', 'Table 2. Foreign Key Association')
  * 3. Because soft deletion is used, ->whereNull('a.deleted_at') should be added when querying
  * 4. Use the DB::raw method to create a native expression and write it into the field name to be queryed
  * ->select(DB::raw(',,b.`realName` as userName,c.`name` as officeName'))
  *5. Use orderBy to sort
  *
  */
     $data=DB::table('biz_attendance_sta as a')
       ->leftJoin('sys_user as b', '', '=', '')
       ->leftJoin('sys_office as c', '', '=', '')
      ->select(DB::raw(',,,,
              b.`realName` as userName,c.`name` as officeName'))
       ->whereNull('a.deleted_at')
       ->orderBy('', 'desc');
 //Use if(!empty(xxx)){} to determine whether the parameters transmitted from the front end are empty. If it is not empty, perform conditional query if it is not empty.     if(!empty($user)){
       $data = $data->where( '',$user);
     }
    if(!empty($office)){
      $data = $data->where( '',$office);
    }
 //Use if(!empty(xxx)){} to determine whether the parameters transmitted from the front end are empty. If it is not empty, fuzzy search and joint query will be performed.    if (!empty($key)) {
      $data = $data->where(function ($query) use ($key) {
        $query->where('', 'like', "%{$key}%")
          ->orWhere('', 'like', "%{$key}%")
          ->orWhere('', 'like', "%{$key}%");
      });
    }
// Use ->paginate(10) to paginate    $results=$data ->paginate(10);
    return $results;
}

For more information about Laravel, readers who are interested in this site can view the topic:Laravel framework introduction and advanced tutorial》、《Summary of excellent development framework for php》、《PHP object-oriented programming tutorial》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php

I hope that this article will be helpful to everyone's PHP programming based on the Laravel framework.