I want to create one where uploading images or uploading files is not necessary.
When I don't include the image file an error like this " Error Call to a member function getClientOriginalName() on null". I want file upload to be optional
I'm new to laravel, so can anyone help me?
this error in here : enter image description here
code in : public function storeproduk(Request $request) and public function updateproduk(Request $request, $id)
source code
ProdukController (insert)
public function storeproduk(Request $request)
{
$this->validate($request, [
'anggota_id' => 'required',
'produk_unggulan' => 'required',
'sosial' => 'required',
'lingkungan' => 'required',
// 'fotoone' => 'required|nullable|mimes:jpeg,png,jpg|max:3048',
// 'fototwo' => 'required|nullable|mimes:jpeg,png,jpg|max:3048',
], $messages = [
'anggota_id.required' => 'Nama Anggota Tidak Boleh Kosong dan Wajib Diisi',
'produk_unggulan.required' => 'Produk Unggulan Tidak Boleh Kosong dan Wajib Diisi',
'sosial.required' => 'Kemanfaatan Sosial Tidak Boleh Kosong dan Wajib Diisi',
'lingkungan.required' => 'Kemanfaatan Lingkungan Tidak Boleh Kosong dan Wajib Di isi',
// 'fotoone.required' => 'Foto #1 tidak boleh kosong | Pilih Foto Terlebih Dahulu',
// 'fototwo.required' => 'Foto #2 tidak boleh kosong | Pilih Foto Terlebih Dahulu',
]);
$nm = $request->fotoone;
$namaFile = $nm->getClientOriginalName();
$nm2 = $request->fototwo;
$namaFile2 = $nm2->getClientOriginalName();
$data = Produk::create([
'anggota_id' => $request->anggota_id,
'produk_unggulan' => $request->produk_unggulan,
'jenis_sayur' => $request->jenis_sayur,
'nama_sayur' => $request->nama_sayur,
'kg_sayur' => $request->kg_sayur,
'total_sayur' => $request->total_sayur,
'jenis_buah' => $request->jenis_buah,
'nama_buah' => $request->nama_buah,
'kg_buah' => $request->kg_buah,
'total_buah' => $request->total_buah,
'jenis_ikan' => $request->jenis_ikan,
'nama_ikan' => $request->nama_ikan,
'kg_ikan' => $request->kg_ikan,
'total_ikan' => $request->total_ikan,
'jenis_ternak' => $request->jenis_ternak,
'nama_ternak' => $request->nama_ternak,
'kg_ternak' => $request->kg_ternak,
'total_ternak' => $request->total_ternak,
'sosial' => $request->sosial,
'lingkungan' => $request->lingkungan,
'fotoone' => $request->fotoone = $namaFile,
'fototwo' => $request->fototwo = $namaFile2,
]);
//BamaraID
// $nm = $request->fotoone;
// $namaFile = $nm->getClientOriginalName();
// $nm2 = $request->fototwo;
// $namaFile2 = $nm2->getClientOriginalName();
// $dtUpload = new Produk;
// $dtUpload->fotoone = $namaFile;
// $dtUpload->fototwo = $namaFile2;
$nm->move(public_path() . '/img', $namaFile);
$nm2->move(public_path() . '/img', $namaFile2);
// if ($request->file('fotoone')) {
// $request->file('fotoone')->move('public/fotoproduksi/', $request->file('fotoone')->getClientOriginalName());
// $data->fotoone = $request->file('fotoone')->getClientOriginalName();
// }
// if ($request->file('fototwo')) {
// $request->file('fototwo')->move('public/fotoproduksi/', $request->file('fototwo')->getClientOriginalName());
// $data->fototwo = $request->file('fototwo')->getClientOriginalName();
// }
$data->save();
return redirect('/dashboard/produk')->with('success', 'Data Berhasil Ditambahkan!');
}
ProdukController (update)
public function updateproduk(Request $request, $id)
{
$this->validate($request, [
'fotoone' => 'required',
'fototwo' => 'required',
], $messages = [
'fotoone.required' => 'Foto #1 tidak boleh kosong | Pilih Foto Terlebih Dahulu',
'fototwo.required' => 'Foto #2 tidak boleh kosong | Pilih Foto Terlebih Dahulu',
]);
$dats = Produk::find($id);
$awal = $dats->fotoone;
$awal2 = $dats->fototwo;
$dt = [
'produk_unggulan' => $request['produk_unggulan'],
'nama_sayur' => $request['nama_sayur'],
'kg_sayur' => $request['kg_sayur'],
'total_sayur' => $request['total_sayur'],
'nama_buah' => $request['nama_buah'],
'kg_buah' => $request['kg_buah'],
'total_buah' => $request['total_buah'],
'nama_ikan' => $request['nama_ikan'],
'kg_ikan' => $request['kg_ikan'],
'total_ikan' => $request['total_ikan'],
'nama_ternak' => $request['nama_ternak'],
'kg_ternak' => $request['kg_ternak'],
'total_ternak' => $request['total_ternak'],
'sosial' => $request['sosial'],
'lingkungan' => $request['lingkungan'],
'fotoone' => $awal,
'fototwo' => $awal2,
];
$request->fotoone->move(public_path() . '/img', $awal);
$request->fototwo->move(public_path() . '/img', $awal2);
$dats->update($dt);
// $dats->update($request->all());
// if ($request->file('fotoone')) {
// $request->file('fotoone')->move('public/fotoproduksi/', $request->file('fotoone')->getClientOriginalName());
// $dats->fotoone = $request->file('fotoone')->getClientOriginalName();
// }
// if ($request->file('fototwo')) {
// $request->file('fototwo')->move('public/fotoproduksi/', $request->file('fototwo')->getClientOriginalName());
// $dats->fototwo = $request->file('fototwo')->getClientOriginalName();
// }
// $dats->save();
return redirect('/dashboard/produk')->with('success', 'Data Berhasil Di Update!');
}
}
View :
<form action="{{ route('storeproduk') }}" method="POST" enctype="multipart/form-data">
@csrf
<div >
<label for="fotoone">Foto Produk #1</label>
{{-- @error('fotoone')
<div >{{ $message }}</div>
@enderror --}}
<input type="file" id="fotoone" name="fotoone" >
</div>
<div >
<label for="fototwo">Foto Produk #2</label>
{{-- @error('fototwo')
<div >{{ $message }}</div>
@enderror --}}
<input type="file" id="fototwo" name="fototwo" >
</div>
CodePudding user response:
You can try this:
$nm = $request->file('fotoone');
$namaFile = '';
if ($nm) {
$name = $nm->getClientOriginalName();
$extension = $nm->getClientOriginalExtension();
$namaFile = $name . '.' . $extension;
}
$nm2 = $request->file('fototwo');
$namaFile2 = '';
if ($nm2) {
$name = $nm2->getClientOriginalName();
$extension = $nm2->getClientOriginalExtension();
$namaFile2 = $name . '.' . $extension;
}