Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Staxidibus_Aeternam's avatar

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');
}
0 likes
23 replies
Glukinho's avatar

Please show corresponding rows from routes/web.php and full file BookController.php.

Snapey's avatar

Your book controller needs something when it is being constructed, that the framework does not know how to create.

More of the error message would help, and as has been asked, the entire controller.

Staxidibus_Aeternam's avatar

this is the full controller

namespace App\Http\Controllers;

use App\Models\Book; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Symfony\Component\HttpFoundation\Response; class BookController extends Controller { public function getBooklist(Request $request, Book $book): RedirectResponse { foreach ($book as $ibooks) { echo $ibooks->author = $request->author; } return redirect('bookshelf.blade.php'); }

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');
}


public function deleteBook(Book $book): RedirectResponse
{
    $book->delete();

    echo ("Book deleted successfully!");

    $nano = time_nanosleep(2, 100000);

    if ($nano === true) {
        echo "Slept for 2 seconds, 100 microseconds.\n";
    } elseif ($nano === false) {
        echo "Sleeping failed.\n";
    } elseif (is_array($nano)) {
        $seconds = $nano['seconds'];
        $nanoseconds = $nano['nanoseconds'];
        echo "Interrupted by a signal.\n";
        echo "Time remaining: $seconds seconds, $nanoseconds nanoseconds.";
    }

    return redirect('/bookshelf');
}

public function updateBook(Request $request): Response
{
    $book = Book::findOrFail($request->id);

    if ($book->update($request->all()) === false) {
        return response(
            "Couldn't update the user with id {$request->id}",
            Response::HTTP_BAD_REQUEST
        );
    }

    return response($book);
}

public function getSingleBook(Request $request, Book $book): Response
{
    $book->get();

    return response($book);
}

}

this is the routes/web.php

Route::get('/', function () { $books = Book::all(); return view('books.index', compact('books')); });

Route::get('/get', [BookController::class, 'getBooklist'])->name('getBooks'); Route::post('/create', [BookController::class, 'storeBook'])->name('newBook'); Route::delete('/delete', [BookController::class, 'deleteBook'])->name('delBook'); Route::patch('/update/3', [BookController::class, 'updateBook'])->name('updateBook'); Route::get('/get/2', [BookController::class, 'getSingleBook'])->name('singleBook');

Glukinho's avatar

@Snapey thanks, I used to put four for some reason, now the life will be a little easier....

Snapey's avatar

I cant see anything here where you use Bookshelf service.

Snapey's avatar

Some tips about your code

if ($book->update($request->all()) === false) {
        return response(
            "Couldn't update the user with id {$request->id}",
            Response::HTTP_BAD_REQUEST
        );
    }

This sort of pattern is useless. If something goes wrong in the update then an exception will be thrown, and your if () test will never execute.

If you want to capture the exception and show a custom message, then use try-catch.

The most likely cause of errors is invalid or incomplete data. Avoid using request->all() and only send validated data to your model.

Staxidibus_Aeternam's avatar

thank you, sorry for the mistake

controller:

this is the routes:

<?php

use App\Http\Controllers\BookController;
use App\Models\Book;
use Illuminate\Support\Facades\Route;



Route::get('/', function () {
    $books = Book::all();
    return view('books.index', compact('books'));
});

Route::get('/get', [BookController::class, 'getBooklist'])->name('getBooks');
Route::post('/create', [BookController::class, 'storeBook'])->name('newBook');
Route::delete('/delete', [BookController::class, 'deleteBook'])->name('delBook');
Route::patch('/update/3', [BookController::class, 'updateBook'])->name('updateBook');
Route::get('/get/2', [BookController::class, 'getSingleBook'])->name('singleBook');

require __DIR__.'/settings.php';
require __DIR__.'/auth.php';
 
Glukinho's avatar

@Staxidibus_Aeternam there is no BookShelf class mentioned in your controller or routes. Have you ever written the class App\Http\Services\Bookshelf by yourself? Does it ever exist? Can you post this file?

Also, what is the exact situation where you get the error?

Staxidibus_Aeternam's avatar

I have written the class by myself, it does exist but it's empty. I get the error when i try to create a new book

Staxidibus_Aeternam's avatar

this is my bookshelf:

<?php

namespace App\Http\Services;

use App\Models\Book;

abstract class Bookshelf
{
   public $bindings = [
    
   ];
}
Glukinho's avatar

@Staxidibus_Aeternam find its usages and remove/comment them, it should resolve the problem.

Pay attention to files:

  • bootstrap/app.php
  • bootstrap/providers.php
  • Providers/AppServiceProvider.php

But maybe you should search in a whole app to find it.

Snapey's avatar

What does it contribute to the project, and how is it instantiated? Sorry, its not a pattern I am familiar with

Staxidibus_Aeternam's avatar

it shouldn't do anything to contribute to the project rn. I never call it in my controller and i'm just trying to create a book, but when i submit the form it gives me this error

srushti_kansagara's avatar

@staxidibus_aeternam is you are getting this error in the in the getBooklist() method ????

also i think you are getBookList() is wrong if you are fetching all the books then it should be like this

public function getBooklist(): RedirectResponse
{
    $books = Book::all(); // Get all books

	//using URL
    return redirect('/bookshelf')->with('books', $books);

	//using route name
    return redirect()->route('get')->with('books', $books);

	//either you can directly pass to the blade file like this 
	return view('bookshelf' , compact('books'));								
}

1 . Incorrect Parameter Usage (Book $book)

  • You're injecting a single Book model, but treating it like a collection in foreach.

  • If you want all books, use Book::all() instead.

Redirecting to a Blade File (Incorrect)

  • redirect('bookshelf.blade.php') is wrong—you should redirect to a route name or URL or direct to the blade file (e.g., /bookshelf) .

as for your default route you can do something like this


Route::get('/', function()  { 
	return redirect()->route('getBooks'));
});

Staxidibus_Aeternam's avatar

@srushti_kansagara i'm not calling that method, but even if laravel checked, i updated the code to redirect to somewhere else, and i still get the same error, i just think i need to put something in my bookshelf services. I just don't know what, but i'll figure it out

JussiMannisto's avatar
Level 50

@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:

  1. Take out the service from wherever you're using it. It doesn't seem to do anything anyway.
  2. Make the class non-abstract.
  3. Create an actual implementation of the class, e.g. class MyBookshelf extends Bookshelf, and register it in a service provider as the Bookshelf class.
Snapey's avatar

if you have any more app code, that you didn't write and you don't know why its there, its probably best to delete it !

1 like

Please or to participate in this conversation.