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.

56 lines
1.3 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 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. class BaseModel extends Model
  6. {
  7. // 打印SQL DB::table('article')->orderBy(DB::raw('rand()'))->toSql();
  8. //获取某一表的所有字段
  9. public static function getColumnListing($table)
  10. {
  11. return Schema::getColumnListing($table);
  12. }
  13. //过滤不是某一表的字段
  14. public static function filterTableColumn($data, $table)
  15. {
  16. $table_column = Schema::getColumnListing($table);
  17. if (!$table_column) {
  18. return $data;
  19. }
  20. foreach ($data as $k => $v) {
  21. if (!in_array($k, $table_column)) {
  22. unset($data[$k]);
  23. }
  24. }
  25. return $data;
  26. }
  27. //获取排序排序
  28. public static function getOrderByData($model, $orderby)
  29. {
  30. if ($orderby == 'rand()')
  31. {
  32. $model = $model->orderBy(\DB::raw('rand()'));
  33. }
  34. else
  35. {
  36. if (count($orderby) == count($orderby, 1))
  37. {
  38. $model = $model->orderBy($orderby[0], $orderby[1]);
  39. }
  40. else
  41. {
  42. foreach ($orderby as $row)
  43. {
  44. $model = $model->orderBy($row[0], $row[1]);
  45. }
  46. }
  47. }
  48. return $model;
  49. }
  50. }