Suppose we created module using php artisan make:crud Faq.
Now we need to generate API for that module
Well it can be done by using following command :-
php artisan make:api Faq
What it does
Creates client consummable API
Creates Restful API
Creates Resource(Resources/Faq/FaqResource) and CollectionResource(Resources/Faq/FaqCollection)
Creates ClientAPIController(Controller/Admin/API/Client/FaqClientAPIController) and RestAPIController(Controller/Admin/API/Restful/FaqRestAPIController)
Client API
If you only want to generate client consummable API only
php artisan make:api Faq --client
ClientAPIController Example
//Controller/Admin/API/Client/FaqClientAPIController
<?php
namespace App\Http\Controllers\Admin\API\Client;
use App\Models\Admin\Faq;
use App\Http\Resources\Faq\FaqCollection;
use App\Http\Resources\Faq\FaqResource;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class FaqClientAPIController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return new FaqCollection(Faq::all());
}
/**
* Display the specified resource.
*
* @param App\Models\Admin\Faq $faq
* @return \Illuminate\Http\Response
*/
public function show(Faq $faq)
{
return new FaqResource($faq);
}
}