public function deleteById($id) { $path = 'web/sample_organic_certificate/' . $id; if (Storage::disk('public')->exists($path)) { Storage::disk('public')->deleteDirectory($path); } $result = SampleOrganicCertificate::find($id)->delete(); return $result; }
public function add($data) { $sample = null; $data['serial_number'] = 1; $year = date('Y'); $last_entered = Sample::where('created_at', 'LIKE', $year . '%')->orderBy('serial_number', 'desc')->first(); if ($last_entered) { $data['serial_number'] = $last_entered->serial_number + 1; } $sample = new Sample($data); $sample->save(); if ($sample->token) { $sample_organic_certificates = SampleOrganicCertificate::whereSampleToken($sample->token)->get(); foreach ($sample_organic_certificates as $sample_organic_certificate) { $sample_organic_certificate->sample_token = null; $sample->sample_organic_certificates()->save($sample_organic_certificate); } $sample_allergen_certificates = SampleAllergenCertificate::whereSampleToken($sample->token)->get(); foreach ($sample_allergen_certificates as $sample_allergen_certificate) { $sample_allergen_certificate->sample_token = null; $sample->sample_allergen_certificates()->save($sample_allergen_certificate); } $sample_vegan_certificates = SampleVeganCertificate::whereSampleToken($sample->token)->get(); foreach ($sample_vegan_certificates as $sample_vegan_certificate) { $sample_vegan_certificate->sample_token = null; $sample->sample_vegan_certificates()->save($sample_vegan_certificate); } $sample_images = SampleImage::whereSampleToken($sample->token)->get(); foreach ($sample_images as $sample_image) { $sample_image->sample_token = null; $sample->sample_images()->save($sample_image); } $label_images = LabelImage::whereSampleToken($sample->token)->get(); foreach ($label_images as $label_image) { $label_image->sample_token = null; $sample->label_images()->save($label_image); } } if ($data['producer_id'] == 'new') { unset($data['producer_id']); $producer = new Producer(); $producer->name = $data['producer__name']; $producer->customer_id = $sample->customer_id; $producer->contact_person = $data['producer__contact_person']; $producer->email = $data['producer__email']; $producer->phone = $data['producer__phone']; $producer->save(); $sample->producer_id = $producer->id; $sample->save(); } if ($data['varietals']) { if (is_array($data['varietals'])) { foreach ($data['varietals'] as $varietal_data) { if ($varietal_data['name'] && $varietal_data['percentage']) { $sample->sample_varietals()->save(new SampleVarietal($varietal_data)); } } } } if ($data['prizes']) { if (is_array($data['prizes'])) { foreach ($data['prizes'] as $prize_data) { if ($prize_data['name']) { $sample_prize = $sample->sample_prizes()->save(new SamplePrize($prize_data)); if ($prize_data['token']) { $sample_prize_files = SamplePrizeFile::whereSamplePrizeToken($prize_data['token'])->get(); foreach ($sample_prize_files as $sample_prize_file) { $sample_prize_file->sample_prize_token = null; $sample_prize_file->sample_prize_id = $sample_prize->id; $sample_prize_file->save(); } } $sample_prize->token = null; $sample_prize->save(); } } } } $sample->token = null; $sample->save(); return $sample; }