Untitled

 avatar
unknown
php
a year ago
3.0 kB
4
Indexable
saat update mas.

```
public function update(Request $request, $id)
    {
        try {
            DB::beginTransaction();
            // get data bastwal untuk diupdate di table pivot bastwal_barang
            $bastwal = Bastwal::findOrFail($id);

            // get data petugas to json
            $data_petugas = json_encode([
                'nama_petugas1' => $request->input('nama_petugas1'),
                'satgas_petugas1' => $request->input('satgas_petugas1'),
                'nama_petugas2' => $request->input('nama_petugas2'),
                'satgas_petugas2' => $request->input('satgas_petugas2'),
            ]);

            // update petugas di table bastwal
            $bastwal->update([
                'petugas' => $data_petugas,
            ]);

            $bastwal->barangBuktis()->chunk(800, function ($barangBuktis) use ($request, $bastwal) {
                foreach ($barangBuktis as $barang) {
                    // get image if exists
                    if ($request->hasFile('image_' . $barang->id)) {
                        $image = $request->file('image_' . $barang->id);
                        $uniqueName = time()  . '.' . $image->getClientOriginalExtension();
                        // compress image with intervention image
                        $manager = new ImageManager(Driver::class);
                        $image_resize = $manager->read($image->getRealPath());
                        $image_resize->scaleDown(null, 800, function ($constraint) {
                            $constraint->aspectRatio();
                        });
                        $directory = env('AWS_FOLDER') . now()->format('Y') . '/' . 'barang/image';
                        // Menyimpan file ke S3
                        $aws = Storage::disk(env('FILESYSTEM_CLOUD'))->put($directory . '/' . $uniqueName, $image_resize->encode());
                        if ($aws) {
                            $urlImage = $directory . '/' . $uniqueName;
                            $barang->image = $urlImage;
                        }
                    }

                    $lokasiPenyimpanan = $request->input('lokasi_' . $barang->id);

                    $barang->lokasi_penyimpanan = $lokasiPenyimpanan;
                    $barang->save();

                    // update data lokasi_penyimpanan dan  ket_penempatan di table pivot bastwal_barang
                    $bastwal->bastwalBarang()->update([
                        'lokasi_penyimpanan' => $barang->lokasi_penyimpanan,
                        'ket_penempatan' => $request->ket_penempatan,
                    ]);

                    // save history form helper
                    SaveHistoryHelper::saveHistory($barang, 'Penempatan Awal', Auth::user()->id);
                }
            });

            DB::commit();
            return redirect()->route('backoffice.penempatan.index')->with('toast_success', 'Data Berhasil dibuat');
        } catch (\Throwable $th) {
            throw $th;
            DB::rollBack();
        }
    }
```
Editor is loading...
Leave a Comment