Create Cors middleware
# php artisan make:middleware Cors
Middleware created successfully.
app/Http/Middleware/Cors.php
public function handle($request, Closure $next)
{
return $next($request)
->header('Access-Control-Allow-Origin', '*')
->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
}
app/Http/Kernel.php $routedMiddleware array add
'cors' => \App\Http\Middleware\Cors::class,
Route/api.php
Route::group(['middleware' => 'cors'], function () {
Route::get('/v1/employees/{id?}', 'Employees@index');
Route::post('/v1/employees', 'Employees@store');
Route::post('/v1/employees/{id}', 'Employees@update');
Route::delete('/v1/employees/{id}', 'Employees@destroy');
});