Answer the question
In order to leave comments, you need to log in
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'
];
}
<?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');
}
}
Route::resource('book', 'BookController');
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question