so, I have two conditions which I wrap using if with "or" operator. previously it worked fine using the "and" operator, but when I only update one file upload it doesn't run, but if both file uploads the update is successful. to cover the shortcoming I thought of replacing it with the "or" operator, but it was an error.
my code,
if (Request()->sampul or Request()->buku <> "") {
//upload gambar/foto dan buku
$file = Request()->sampul;
$fileName = Request()->slug . '.' . $file->extension(); //rename nama berdasarkan slug
$file->move(public_path('file_buku'), $fileName);
$file1 = Request()->buku;
$fileName1 = Request()->nama . '.' . $file1->extension(); //rename nama berdasarkan nama
$file1->move(public_path('file_buku'), $fileName1);
$data = [
'nama' => Request()->nama,
'slug' => Request()->slug,
'created_at' => Request()->created_at,
'sampul' => $fileName,
'buku' => $fileName1
];
$this->BukuModel->editData($id_buku, $data);
} else {
//Jika tidak ingin ganti sampul dan buku
$data = [
'nama' => Request()->nama,
'slug' => Request()->slug,
'created_at' => Request()->created_at
];
$this->BukuModel->editData($id_buku, $data);
}
if (Request()->sampul or Request()->buku <> "") {
//upload gambar/foto dan buku
$file = Request()->sampul; //THIS IS NULL
$fileName = Request()->slug . '.' . $file->extension(); //
What your code is doing is run the if
block if there is any non empty data for sampul or buku is not "" .So if any one of those 2 condition passed then the if block will run.
And your Request()->buku <> ""
condition passed which means Request()->sampul
is empty/null.
$file = Request()->sampul; //THIS IS NULL
$fileName = Request()->slug . '.' . $file->extension(); //
Now in this code block you are trying to access the extension() from the null/empty value. So you are getting the error.
Your code should be something like:
if (Request()->sampul || Request()->buku !== "") {
//upload gambar/foto dan buku
if (Request()->sampul) {
$file = Request()->sampul;
$fileName = Request()->slug.'.'.$file->extension(); //rename nama berdasarkan slug
$file->move(public_path('file_buku'), $fileName);
}
if (Request()->buku) {
$file1 = Request()->buku;
$fileName1 = Request()->nama.'.'.$file1->extension(); //rename nama berdasarkan nama
$file1->move(public_path('file_buku'), $fileName1);
}
$data = [
'nama' => Request()->nama,
'slug' => Request()->slug,
'created_at' => Request()->created_at,
'sampul' => $fileName,
'buku' => $fileName1
];
} else {
//Jika tidak ingin ganti sampul dan buku
$data = [
'nama' => Request()->nama,
'slug' => Request()->slug,
'created_at' => Request()->created_at
];
}
$this->BukuModel->editData($id_buku, $data);
Also, using or
and and
is not a good practise. Try to use ||
and '&&' instead.