I'm working on a REST API with laravel.
I have a blogs table
Schema::create('blogs', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->longtext('body');
$table->string('friendly_url');
});
I have a my route set up for the show controller that will display the blog searched by id
Route
Route::get('/{id}', 'BlogController@show');
Controller
public function show($id)
{
$blog = Blog::find($id);
if (!$blog) {
return response()->json([
'message' => '404 Not Found'
], 400);
}
return response()->json($blog, 200);
}
So by accessing
/api/blog/1
I get
{
"id": 1,
"title": "title of my blog",
"body": "conteudo do meu blog",
"friendly_url": "title-of-my-blog",
"category_id": 2
}
but I want to check the blog also by the friendly URL
/api/blog/{friendly-url} OR {id}
/api/blog/title-of-my-blog
and get the same result
I would like to know the best practice to do this, someone to help?