DistProduct.php 1.5 KB

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