JackpotK
JackpotK

Reputation: 313

Laravel 4 resource controller show($id) method problems

Does it have to be the 'id' column in database table which works fine for show($id), edit($id) method in controller?
I want to replace $id with the value of 'post_id' column in database table, but it throws error: ModelNotFoundException
How can I fix it?

sample code:

database table:

id(int), post_id(varchar32), post_title(varchar32), post_content(text)

routes:

Route::resource('posts', 'PostsController');

PostsController:

public function show($id) { return View::make('posts.show'); }

When I visit http://localhost/posts/1 it should return the view of post which has id 1 in the table.
What if I what to return the view based on post_id value in the table?
Does it have to replace the parameter in show() or ?

Upvotes: 0

Views: 5575

Answers (2)

abhayendra
abhayendra

Reputation: 187

you can also use find

public function show($id) { 
$post = Post::find($id);
return View::make('posts.show', compact('post')); 
}

Upvotes: 0

Dustin Fraker
Dustin Fraker

Reputation: 534

In your PostsController you need to access the Post model to get the data from the db.

like this: //PostsController

public function show($id) { 
$post = Post::where('post_id', $id)->first();

return View::make('posts.show', compact('post')); 
}

Upvotes: 2

Related Questions