Last active
August 10, 2018 21:08
-
-
Save tonysm/0c725c5a58f67240eb765136c0343b0e to your computer and use it in GitHub Desktop.
Nova API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use App\Api\ApiResource; | |
use App\Nova\UserResource; | |
class ApiResourcesServiceProvider extends ServiceProvider | |
{ | |
public function boot() | |
{ | |
ApiResource::register([ | |
UserResource::class, | |
]); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ artisan route:list | |
+--------+-----------+------------------+---------------+----------------------------------------------+--------------+ | |
| Domain | Method | URI | Name | Action | Middleware | | |
+--------+-----------+------------------+---------------+----------------------------------------------+--------------+ | |
| | GET|HEAD | / | | Closure | web | | |
| | GET|HEAD | api/users | users.index | App\Http\Controllers\UsersController@index | api,auth:api | | |
| | POST | api/users | users.store | App\Http\Controllers\UsersController@store | api,auth:api | | |
| | GET|HEAD | api/users/{user} | users.show | App\Http\Controllers\UsersController@show | api,auth:api | | |
| | PUT|PATCH | api/users/{user} | users.update | App\Http\Controllers\UsersController@update | api,auth:api | | |
| | DELETE | api/users/{user} | users.destroy | App\Http\Controllers\UsersController@destroy | api,auth:api | | |
+--------+-----------+------------------+---------------+----------------------------------------------+--------------+ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Nova; | |
// ... other imports | |
use Illuminate\Routing\Router; | |
class UserResource extends NovaResource | |
{ | |
/** | |
* The model the resource corresponds to. | |
* | |
* @var string | |
*/ | |
public static $model = 'App\User'; | |
public function routes(Router $router) | |
{ | |
$router->group(['namespace' => 'App\Api\Http\Controllers'], function (Router $router) { | |
$router->apiResource('users', 'UsersController'); | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Nova; | |
use Laravel\Nova\Fields\ID; | |
use Laravel\Nova\Fields\Text; | |
use Laravel\Nova\Fields\DateTime; | |
class UserResource extends NovaResource | |
{ | |
/** | |
* The model the resource corresponds to. | |
* | |
* @var string | |
*/ | |
public static $model = 'App\User'; | |
/** | |
* Get the fields displayed by the resource. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @return array | |
*/ | |
public function fields(Request $request) | |
{ | |
return [ | |
ID::make()->sortable(), | |
Text::make('Name')->sortable()->rules('required', 'max:255'), | |
Text::make('Email') | |
->sortable() | |
->rules('required', 'email', 'max:255') | |
->creationRules('unique:users,email') | |
->updateRules('unique:users,email,{{resourceId}}'), | |
DateTime::make('Created At'), | |
]; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @apiDoc \App\Nova\UserResource | |
* @apiDocExampleName Create users successfully | |
*/ | |
public function testCreatesUsersSuccessfully() | |
{ | |
$response = $this->postJson(route('users.store'), [ | |
'name' => 'John Doe', | |
'email' => '[email protected]', | |
]); | |
$response->assertStatus(201); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment