PHP code example of czim / laravel-jsonapi

1. Go to this page and download the library: Download czim/laravel-jsonapi 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/ */

    

czim / laravel-jsonapi example snippets




    public function render($request, Exception $exception)
    {
        if (is_jsonapi_request() || $request->wantsJson()) {
            return jsonapi_error($exception);
        }
        
        // ...


    protected $middlewareGroups = [
        'api' => [
            // ... 
            \Czim\JsonApi\Http\Middleware\RequireJsonApiHeader::class,
        ],
    ];


    // Get the full filter data associative array.
    $filter = jsonapi_query()->getFilter();
    
    // Get a specific filter key value, if it is present (with a default fallback).
    $id = jsonapi_query()->getFilterValue('id', 0);
    
    // Get the page number.
    $page = jsonapi_query()->getPageNumber();


    // Using the interface binding ...
    $jsonapi = app(\Czim\JsonApi\Contracts\Support\Request\RequestQueryParserInterface::class);
    
    // Or by instantiating it manually ...
    $jsonapi = new \Czim\JsonApi\Support\Request\RequestQueryParser(request());
    
    // After this, the same methods are available
    $id = $jsonapi->getFilterValue('id');


    // Get the root type of the object (which may be 'resource', 'error' or 'meta').
    $rootType = jsonapi_request()->data()->getRootType();
    
    // Get validated data for the current request.
    // This returns an instance of \Czim\JsonApi\Data\Root, which is a data object tree.
    $root = jsonapi_request()->data();
    
    // You can check what kind of resource data is contained.
    if ( ! $root->hasSingleResourceData()) {
        // In this case, the request would either have no "data" key,
        // or it would contain NULL or an array of multiple resources.
    } elseif ($root->hasMultipleResourceData()) {
        // In this case, the request has a "data" key that contains an array of resources.
    }
    
    // Embedded data may be accessed as follows (for single resource).
    $resourceId     = $root->data->id;
    $resourceType   = $root->data->type; 
    $attributeValue = $root->data->attributes->name;
    $relationType   = $root->data->relationships['some-relationship']->data->type;


    'map' => [
        \Your\ContentClassFqn\Here::class => \Your\TransformerClassFqn\Here::class,        
    ],
 php
Czim\JsonApi\Providers\JsonApiServiceProvider::class,
 bash
php artisan vendor:publish