H
H
HeartOfProgrammer2016-02-28 07:31:32
Laravel
HeartOfProgrammer, 2016-02-28 07:31:32

Why is Book model not found in laravel?

My laravel version is 5.1, I'm doing CRUD according to this article .
Created a model via php artisan make:model Book
And added the following code there:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Book extends Model
{
    protected $table = 'books';
    protected $fillable = [
        'title',
        'description',
        'author'
    ];
}

I also created the controller using artisan. I
added the following code there:
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class BookController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $allBooks = Book::all();

        return view('books.bookList', compact('allBooks'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('books.addBook');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(PublishBookRequest $requestData)
    {
        $book = new Book;
        $book->title = $requestData['title'];
        $book->description = $requestData['description'];
        $book->author = $requestData['author'];
        $book->save();

        return redirect()->route('book.index');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $book = Book::find($id);

        return view('books.showBook');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $book = Book::find($id);

        return view('books.editBook');
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update($id, PublishBookRequest $requestData)
    {
        $book = Book::find($id);

        $book->title = $requestData['title'];
        $book->description = $requestData['description'];
        $book->author = $requestData['author'];
        $book->save();

        return redirect()->route('book.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Book::find($id)->delete();

        return redirect()->route('book.index');
    }
}

The router is written like this:
Route::resource('book', 'BookController');
The problem is that when you enter an address, for example , crud.local/book , it gives me an error like the "Book" model was not found
090c350956b74a1688cd59322b27d11f.png
, but if you go to http:://crud.local/book/create, then everything comes in, shows a form and a button.
Here are my routes
201a77922db54a918b389ba33baa12fd.png
Maybe I'm not writing the model correctly?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Ukolov, 2016-02-28
@HeartOfProgrammer

use App\Book;

O
OnYourLips, 2016-02-28
@OnYourLips

Use an IDE.
The environment will show you your errors (you forgot to include the class) as soon as you write the code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question