Muhammad Arslan Muhammad Arslan - 5 months ago 7x
PHP Question

I am learning laravel and i want to try show a single book through a wildcard but it shows an error

here is my route

Route::get('showbooks', 'AdminController@show');
Route::get('showbooks/{book_id}', 'AdminController@singleBook');

here is methods for all and single book

public function show()
$allBooks = Book::all();
return view ('admin.showbooks', compact('allBooks'));

public function singleBook($book_id)
$book = Book::find($book_id);
return view ('admin.singlebook', compact('book'));

here is link for single book

@foreach($allBooks as $book)
<h2><a href="{{ url('showbooks', $book->book_id) }}"> Book Id is: {{ $book->book_id }}</a></h2>

<h4>Book Title is: {{ $book->title }}</h4>

this is my single book view

<div class="col-lg-9">
<h1>{{ $book->book_id }}</h1>
<h3>{{ $book->tile }}</h3>


You should set primary key in your model. Because by default primary key is taken as "id".

Thats why when you write

$book = Book::find($book_id);

laravel makes sql as


Change your model as below:

class Book extends Eloquent {

    protected $primaryKey = 'book_id';