PHP code example of grantholle / api-resource-detection
1. Go to this page and download the library: Download grantholle/api-resource-detection 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/ */
grantholle / api-resource-detection example snippets
namespace App\Models;
use GrantHolle\Http\Resources\Traits\HasResource;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use HasResource;
protected $guarded = [];
}
$user = User::find(1);
// This will be a UserResource instance
$user->toResource();
$user = User::find(1);
User::resource($user);
$users = User::all();
User::resource($users);
public function boot() {
\GrantHolle\Http\Resources\JsonResource::resolveResourceNamespaceUsing(function () {
return 'My\\Resource\\Namespace\\';
});
}
public function boot() {
\GrantHolle\Http\Resources\JsonResource::guessResourceNamesUsing(function ($modelClass) {
$modelName = class_basename($modelClass);
// This will return a resource with the class name
// ModelApiResource in the `My\Resource\Namespace` namespace.
return 'My\\Resource\\Namespace\\' . $modelName . 'ApiResource';
});
\GrantHolle\Http\Resources\JsonResource::guessResourceCollectionNamesUsing(function ($modelClass) {
$modelName = class_basename($modelClass);
// This will return a resource with the class name
// ModelCollectionApiResource in the `My\Resource\Namespace` namespace.
return 'My\\Resource\\Namespace\\' . $modelName . 'CollectionApiResource';
});
}
php artisan make:resource UserResource
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.