1. Go to this page and download the library: Download ark4ne/laravel-json-api library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ark4ne / laravel-json-api example snippets
use \Ark4ne\JsonApi\Requests\Rules\Includes;
use \Illuminate\Foundation\Http\FormRequest;
class UserFetchRequest extends FormRequest
{
public function rules()
{
return [
'
use \Ark4ne\JsonApi\Requests\Rules\Fields;
use \Illuminate\Foundation\Http\FormRequest;
class UserFetchRequest extends FormRequest
{
public function rules()
{
return [
'fields' => [new Fields(UserResource::class)],
]
}
}
protected function toType(Request $request): string;
protected function toIdentifier(Request $request): int|string;
public function toAttributes(Request $request): iterable;
protected function toRelationships(Request $request): iterable;
protected function toResourceMeta(Request $request): ?iterable;
protected function toMeta(Request $request): ?iterable;
/// Role.php
enum Role {
case ADMIN;
case USER;
}
/// State.php
enum State:int {
case ACTIVE = 1;
case INACTIVE = 0;
}
/// User.php
class User extends Model
{
$casts = [
'role' => Role::class,
'state' => State::class,
];
}
// UserResource.php
public function toAttributes(Request $request): array
{
return [
'status' => $this->enum(),
'role' => $this->enum(),
];
}
[
"status": 1,
"role": "ADMIN"
]
// UserResource.php
public function toAttributes(Request $request): array
{
return [
// Array of strings - all values will be cast to string
'tags' => $this->array('tags')->of($this->string()),
// Array of integers - all values will be cast to integer
'scores' => $this->array('scores')->of($this->integer()),
// Array of floats
'prices' => $this->array('prices')->of($this->float()),
// Array of booleans
'flags' => $this->array('flags')->of($this->bool()),
];
}
use Ark4ne\JsonApi\Descriptors\Values\ValueString;
use Ark4ne\JsonApi\Descriptors\Values\ValueInteger;
public function toAttributes(Request $request): array
{
return [
'tags' => $this->array('tags')->of(ValueString::class),
'scores' => $this->array('scores')->of(ValueInteger::class),
];
}
public function toAttributes(Request $request): array
{
return [
'tags' => $this->arrayOf($this->string(), 'tags'),
'scores' => $this->arrayOf($this->integer(), 'scores'),
];
}
public function toAttributes(Request $request): array
{
return [
// 2D array (matrix) of integers
'matrix' => $this->array('matrix')->of(
$this->array()->of($this->integer())
),
];
}
public function toAttributes(Request $request): array
{
return [
// Transform and type cast
'doubled' => $this->array(fn() => array_map(fn($n) => $n * 2, $this->numbers))
->of($this->integer()),
// Access nested properties
'user_ids' => $this->array(fn() => $this->users->pluck('id'))
->of($this->integer()),
];
}
public function toAttributes(Request $request): array
{
return [
// Only // Only onditional based on closure
'admin_notes' => $this->array('notes')->of($this->string())
->when(fn() => $request->user()->isAdmin()),
];
}
> // ❌ This will NOT filter individual items
> 'even-numbers' => $this->array('numbers')->of(
> $this->integer()->when(fn($request, $model, $attr) => $attr % 2 === 0)
> )
> // All items will be