DistProduct.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace App\Distributor\Repositories;
  3. use Dcat\Admin\Form;
  4. use App\Models\DistProduct as Model;
  5. use Dcat\Admin\Repositories\EloquentRepository;
  6. class DistProduct extends EloquentRepository
  7. {
  8. /**
  9. * Model.
  10. *
  11. * @var string
  12. */
  13. protected $eloquentClass = Model::class;
  14. public function delete(Form $form, array $originalData)
  15. {
  16. collect(explode(',', $form->getKey()))->filter()->each(function ($id) {
  17. $product = Model::find($id);
  18. if ($product) {
  19. // 验证 dist_id
  20. if ($product->dist_id !== getDistributorId()) {
  21. throw new \Exception('Unable to modify the product because the distributor ID does not match.');
  22. return;
  23. }
  24. Model::find($id)->images()->delete();
  25. Model::find($id)->delete();
  26. }
  27. });
  28. return true;
  29. }
  30. public static function findById($id)
  31. {
  32. return Model::find($id); // 查找并返回相应的记录
  33. }
  34. /*
  35. * 查找最新的N个产品
  36. */
  37. public static function selectOptionsNew($limit=30)
  38. {
  39. return Model::where('dist_id', getDistributorId())->where('enabled', 1)->orderBy('created_at', 'desc')->limit($limit)->pluck('title', 'id');
  40. }
  41. /*
  42. * 获取一个标签
  43. */
  44. public static function getOneById($id)
  45. {
  46. return Model::where('id', $id)->where('dist_id', getDistributorId())->first();
  47. }
  48. }