Hello artisan,
In this tutorial, I will show you how to create rest api using laravel orion package. Laravel Orion uses the most powerful features of Laravel like Eloquent models and relationships, policies, request classes, and API resources, which makes it more powerful and extensible, yet simple to get started with. We can create rest api using laravel orion very quickly.
If you want to create rest api crud within a few minutes then laravel orion is for you. It gives more powerful features with a sort statement of code. In this example, I will show you how to create a complete crud application in Laravel 9 using laravel orion package.
In this example, I will also show laravel orion validation example. Look at that, even taylor otowell gives a star on laravel orion github repo. So let's check the package.
Step 1: Install Laravel Orion Package
Laravel Orion can be installed into a new or existing project, simply by running the below command:
composer require tailflow/laravel-orion
//then
php artisan vendor:publish --tag=orion-config
Read also: Fetch HTTP Client Response as Collection in Laravel 9
Step 2: Add Route
Now in this step, register the route in api.php
by calling Orion::resource
routes/api.php
use Orion\Facades\Orion;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
Route::group(['as' => 'api.'], function() {
Orion::resource('users', UserController::class);
});
Now php artisan route:list
you will see the all route of user from laravel orion like:
Step 3: Create Controller
Let's assume you have a model User that represents users and you would like to manage it via REST API. With Laravel Orion it can be accomplished in 3 simple steps:
app/Http/Controller/UserController.php
namespace App\Http\Controllers;
use App\Http\Requests\UserRequest;
use App\Models\User;
use Orion\Concerns\DisablePagination;
use Orion\Http\Controllers\Controller;
use Orion\Concerns\DisableAuthorization;
class UserController extends Controller
{
use DisableAuthorization, DisablePagination;
protected $model = User::class;
protected $request = UserRequest::class;
}
Step 4: Create Validation Request
Defining rules for store
and update
operations in Laravel Orion, it provides Orion\Http\Requests\Request
class with a handful of methods to specify validation rules. Now create a rule by below command:
php artisan make:request UserRequest
And update it like this:
App\Http\Requests\UserRequest.php
namespace App\Http\Requests;
use Orion\Http\Requests\Request;
class UserRequest extends Request
{
public function authorize()
{
return true;
}
public function storeRules() : array
{
return [
'name' => 'required',
'email' => 'required|unique:users',
'password' => 'required'
];
}
}
Read also: How to Retry HTTP Request until There is a Result in Laravel
Now all are set to go. Let's check the API request via the postman.
Read also: Handle Exception Error in Laravel using Rescue Helper
Look at that. How cool it is. We can use relationship, validation, and almost all of the laravel features.
#laravel #laravel-9x