<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Company;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\File;
class CompanyController extends Controller
{
//
public function index()
{
$data['company'] = Company::where('id', 1)->first();
$data['title'] = "company's info";
return view('company.index', $data);
}
public function update(Request $request, string $id)
{
try {
$decryptId = Crypt::decryptString($id);
$request->validate([
'name' => 'required',
'email' => 'required|email',
'nomorhp' => 'required',
'alamat' => 'required',
'kota' => 'required',
'kecamatan' => 'required',
'provinsi' => 'required',
'photo' => 'mimes:jpg,jpege,png,gif,svg'
]);
$data = [
'nama' => $request->name,
'email' => $request->email,
'nohp' => $request->nomorhp,
'alamat' => $request->alamat,
'kota' => $request->kota,
'kecamatan' => $request->kecamatan,
'provinsi' => $request->provinsi
];
if ($request->hasFile('photo')) {
$foto_file = $request->file('photo');
$foto_ekstensi = $foto_file->extension();
$fotobaru = "Logo-" . date('ymdhis') . "." . $foto_ekstensi;
$foto_file->move(public_path('images'), $fotobaru);
$data['photo'] = $fotobaru;
$Company = Company::where('id', $decryptId)->first();
$foto_lama = $Company->photo;
File::delete(public_path('images') . "/" . $foto_lama);
}
Company::where('id', $decryptId)->update($data);
return redirect('/company')->with('success', 'berhasil update info company');
} catch (DecryptException $e) {
return redirect('/company')->with('error', 'Invalid ID');
}
}
}
Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]