DistProduct.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. }