Laravel makes creating WebAPI to connect with Angular, VUE, React, Next.js real easy.
Code focuses Laravel8 onwards and targets receiving JSON data thru WebAPI without going into security complications.
Pre-readiness: Database/table with the name 'posts' is ready and filled with required data
Step1: Create controller names Blog -> php artisan make:controller Blog
Step2: Open the controller and place the following code:
use DB;
class Blog extends Controller
{
function postapi(){
$rs = DB::table('posts')->get();
return response()->json($rs);
}
}
Step3: Open routes/web.php and place the following code:
Route::controller(Blog::class)->group(function () {
Route::get('/postapi', 'postapi');
});
Step4: localhost/projectname/postapi