Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Last active January 20, 2022 21:02
Show Gist options
  • Save Shelob9/492690fa2b0625b6a5689deb6e8d3c02 to your computer and use it in GitHub Desktop.
Save Shelob9/492690fa2b0625b6a5689deb6e8d3c02 to your computer and use it in GitHub Desktop.
Laravel Spark with Laravel Nova
php artisan nova:resource Team
php artisan nova:resource Subscription
<?php
namespace App\Nova;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\Date;
use Laravel\Nova\Fields\ID;
use Illuminate\Http\Request;
use Laravel\Nova\Http\Requests\NovaRequest;
class Subscription extends Resource
{
/**
* The model the resource corresponds to.
*
* @var string
*/
public static $model = '\Laravel\Spark\Subscription';
/**
* Get the fields displayed by the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function fields(Request $request)
{
return [
//Subscription ID
ID::make()->sortable(),
//Created
Date::make('Created', 'created_at')
->sortable(),
//Trial End Date
Date::make('Trial Ends', 'trial_ends_at')
->sortable(),
];
}
}
<?php
namespace App\Nova;
use Illuminate\Support\Str;
use Laravel\Nova\Fields\BelongsToMany;
use Laravel\Nova\Fields\ID;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\Select;
use Laravel\Nova\Fields\Text;
use Laravel\Spark\Spark;
class Team extends Resource {
/**
* The columns that should be searched.
*
* @var array
*/
public static $search = [
'id',
'name'
];
/**
* Get the fields displayed by the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function fields(Request $request)
{
return [
//Team ID
ID::make()->sortable(),
//Team name
Text::make('Name')
->sortable()
->rules('required', 'max:255'),
//Team Slug
Text::make('Slug'),
//Subscription plans for team
HasMany::make('Subscriptions'),
//Team members
BelongsToMany::make('Team Members', 'users', 'App\Nova\User')
->fields(function () {
//User role
return [
Select::make('Role')
->options(
array_merge(
//@see https://spark.laravel.com/docs/8.0/teams#team-roles
Spark::roles(),
[
//default role
Spark::defaultRole() => Str::ucfirst(Spark::defaultRole()),
//owner role
'owner' => 'Owner'
]
)
)
];
})
];
}
<?php
namespace App\Nova;
use App\Team;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\BelongsToMany;
use Laravel\Nova\Fields\HasMany;
use Laravel\Nova\Fields\ID;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\Gravatar;
use Laravel\Nova\Fields\Password;
class User extends Resource
{
/**
* The model the resource corresponds to.
*
* @var string
*/
public static $model = 'App\\User';
/**
* The columns that should be searched.
*
* @var array
*/
public static $search = [
'id', 'name', 'email',
];
/**
* Get the fields displayed by the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function fields(Request $request)
{
return [
//User Id
ID::make()->sortable(),
//user gravatar
Gravatar::make(),
//User's spark team
BelongsToMany::make('Teams'),
//User name
Text::make('Name')
->sortable()
->rules('required', 'max:255'),
//User email
Text::make('Email')
->sortable()
->rules('required', 'email', 'max:254')
->creationRules('unique:users,email')
->updateRules('unique:users,email,{{resourceId}}'),
//User password
Password::make('Password')
->onlyOnForms()
->creationRules('required', 'string', 'min:8')
->updateRules('nullable', 'string', 'min:8'),
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment