You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.5 KiB

7 years ago
6 years ago
7 years ago
7 years ago
6 years ago
6 years ago
7 years ago
7 years ago
6 years ago
7 years ago
6 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Model;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\Schema;
  5. use DB;
  6. use Log;
  7. class BaseModel extends Model
  8. {
  9. // 打印SQL DB::table('article')->orderBy(DB::raw('rand()'))->toSql();
  10. //获取某一表的所有字段
  11. public static function getColumnListing($table)
  12. {
  13. return Schema::getColumnListing($table);
  14. }
  15. //过滤不是某一表的字段、过滤空值
  16. public static function filterTableColumn($data, $table)
  17. {
  18. $table_column = Schema::getColumnListing($table);
  19. if (!$table_column)
  20. {
  21. return $data;
  22. }
  23. foreach ($data as $k => $v)
  24. {
  25. if (!in_array($k, $table_column))
  26. {
  27. unset($data[$k]);
  28. }
  29. else
  30. {
  31. if($data[$k]==''){unset($data[$k]);} //过滤空值
  32. }
  33. }
  34. return $data;
  35. }
  36. //获取排序参数
  37. public static function getOrderByData($model, $orderby)
  38. {
  39. if ($orderby == 'rand()')
  40. {
  41. $model = $model->orderBy(\DB::raw('rand()'));
  42. }
  43. else
  44. {
  45. if (count($orderby) == count($orderby, 1))
  46. {
  47. $model = $model->orderBy($orderby[0], $orderby[1]);
  48. }
  49. else
  50. {
  51. foreach ($orderby as $row)
  52. {
  53. $model = $model->orderBy($row[0], $row[1]);
  54. }
  55. }
  56. }
  57. return $model;
  58. }
  59. }