Please show corresponding rows from routes/web.php and full file BookController.php.
Target [App\Http\Services\Bookshelf] is not instantiable while building [App\Http\Controllers\BookController].
I've been having problems creating an instance of a new book. I'm new to laravel so there isn't much in my app
This is my book controller function to create a book
public function storeBook(Request $request)
{
$validated = $request->validate([
'title' => 'required|string|max:255',
'summary' => 'required',
'pages' => 'required|integer',
'genre' => 'required|string',
'author' => 'required|string',
'color' => 'required|string',
'price' => 'required|numeric',
'photo' => 'nullable|image|max:2048',
]);
if ($request->hasFile('photo')) {
$filename = time() . '.' . $request->photo->extension();
$request->photo->move(public_path('images'), $filename);
$validated['photo'] = $filename;
}
Book::create($validated);
return redirect()->back()->with('success', 'Book added');
}
@Staxidibus_Aeternam Laravel is trying to instantiate the Bookshelf service, probably because you've type-hinted the class somewhere, but you can't instantiate an abstract class.
You haven't shown where you've used the Bookshelf class, but clearly it's somewhere because Laravel is trying to resolve it. It might be in a constructor somewhere. Do a project-wide search or use your IDE tools to find it.
When you've type-hinted the class in some component, Laravel looks for a matching service in the service container. Because you haven't registered one, Laravel is trying to instantiate the class itself. That doesn't work because it's marked abstract, and you get an error.
You're choices are:
- Take out the service from wherever you're using it. It doesn't seem to do anything anyway.
- Make the class non-abstract.
- Create an actual implementation of the class, e.g.
class MyBookshelf extends Bookshelf, and register it in a service provider as theBookshelfclass.
Please or to participate in this conversation.