So I imported my laravel project in web hostinger for web hosting, upon adding a book which consists of uploading an image, it reflects on the database but it does not store the image. I used php artisan storage:link in my laravel project.
public function store(Request $request)
{
if ($request->has('photo')) {
$file = $request->file('photo');
$extension = $file->getClientOriginalExtension();
$filename = time() . '.' . $extension;
$file->storeAs('storage/' . $filename);
}
// Create a new book
$book = Book::create([
'title' => $request->title,
'author' => $request->author,
'category_id' => $request->category_id,
'quantity' => $request->quantity,
'available_books' => $request->quantity, // Set available_books equal to quantity
'isbn' => $request->isbn,
'photo' => $filename ? $filename : null,
]);
$book->save();
return redirect()->route('admin.dashboard')->with('success_add', 'Book added successfully!');
}
public function update(Request $request, $id)
{
$book = Book::findOrFail($id);
// Update fields
$book->title = $request->input('title');
$book->author = $request->input('author');
$book->category_id = $request->input('category_id');
$book->quantity = $request->input('quantity');
$book->isbn = $request->input('isbn');
// Handle image upload
if ($request->hasFile('image')) {
// Delete previous image if exists
if ($book->photo) {
Storage::delete('storage/' . $book->photo);
}
// Upload new image
$file = $request->file('image');
$extension = $file->getClientOriginalExtension();
$filename = time() . '.' . $extension;
$file->storeAs('storage/', $filename);
$book->photo = $filename;
}
$book->save();
return redirect()->route('admin.dashboard')->with([
'success_inv' => 'Book updated successfully',
'updatedBookId' => $id
]);
}
I tried to change
$file->storeAs('storage/' . $filename);
to
$file->storeAs('public/' . $filename);
but still does not store the image uploaded
New contributor
xDarryl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.