Skip to content

Commit

Permalink
docs: add Workbench examples to use the field with Flexible and Repeater
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelpopowicz committed Jun 21, 2024
1 parent 25faafc commit 0a1f220
Show file tree
Hide file tree
Showing 12 changed files with 362 additions and 7 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"pestphp/pest": "^1.21 || ^2.0",
"pestphp/pest-plugin-laravel": "^1.2 || ^2.0",
"pestphp/pest-plugin-mock": "^1.0 || ^2.0",
"spatie/laravel-ray": "^1.29"
"spatie/laravel-ray": "^1.29",
"whitecube/nova-flexible-content": "^1.1"
},
"autoload": {
"psr-4": {
Expand Down
73 changes: 72 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion testbench.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ providers:
- Oneduo\NovaFileManager\ToolServiceProvider
- Workbench\App\Providers\NovaServiceProvider

migrations: true
migrations:
- workbench/database/migrations

seeders:
- Workbench\Database\Seeders\DatabaseSeeder

workbench:
start: /nova
user: [email protected]
build:
- asset-publish
- create-sqlite-db
- db:wipe
- migrate:refresh
- storage:link
assets:
- nova-assets
sync: []
Expand All @@ -23,3 +26,7 @@ purge:
directories:
- lang/*
- public/vendor/*
env:
- AUTH_MODEL=Workbench\App\Models\User
- FILESYSTEM_DRIVER=public
- APP_URL=http://localhost:8000
25 changes: 25 additions & 0 deletions workbench/app/Models/Page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Workbench\App\Models;

use Illuminate\Database\Eloquent\Model;
use Oneduo\NovaFileManager\Casts\Asset;
use Whitecube\NovaFlexibleContent\Concerns\HasFlexible;
use Whitecube\NovaFlexibleContent\Value\FlexibleCast;

class Page extends Model
{
use HasFlexible;

protected $fillable = [
'title',
'slug',
'image',
'content',
];

protected $casts = [
'image' => Asset::class,
'content' => FlexibleCast::class,
];
}
20 changes: 20 additions & 0 deletions workbench/app/Models/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Workbench\App\Models;

use Illuminate\Foundation\Auth\User as Authenticable;

class User extends Authenticable
{
protected $fillable = [
'name',
'email',
'email_verified_at',
'password',
'profile',
];

protected $casts = [
'profile' => 'array',
];
}
23 changes: 23 additions & 0 deletions workbench/app/Nova/Flexible/Layouts/ImageAndCaptionLayout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Workbench\App\Nova\Flexible\Layouts;

use Laravel\Nova\Fields\Text;
use Oneduo\NovaFileManager\FileManager;
use Whitecube\NovaFlexibleContent\Layouts\Layout;

class ImageAndCaptionLayout extends Layout
{
protected $name = 'imageandcaptionlayout';

protected $title = 'ImageAndCaptionLayout';

public function fields(): array
{
return [
FileManager::make('Image', 'image')->nullable(),
Text::make('Caption', 'caption')->nullable(),
];
}

}
111 changes: 111 additions & 0 deletions workbench/app/Nova/Page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace Workbench\App\Nova;

use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Markdown;
use Laravel\Nova\Fields\Slug;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Panel;
use Laravel\Nova\Resource;
use Oneduo\NovaFileManager\FileManager;
use Whitecube\NovaFlexibleContent\Flexible;
use Workbench\App\Nova\Flexible\Layouts\ImageAndCaptionLayout;

class Page extends Resource
{
/**
* The model the resource corresponds to.
*
* @var string
*/
public static $model = \Workbench\App\Models\Page::class;

/**
* The single value that should be used to represent the resource when being displayed.
*
* @var string
*/
public static $title = 'title';

/**
* The columns that should be searched.
*
* @var array
*/
public static $search = [
'id', 'title',
];

/**
* Get the fields displayed by the resource.
*
* @return array
*/
public function fields(NovaRequest $request)
{
return [
ID::make()->sortable(),

Text::make('Title')
->sortable()
->rules('required', 'max:255'),

Slug::make('Slug')
->from('Title')
->rules('required', 'max:255'),

FileManager::make('Image')
->rules('required'),

Panel::make('Content', [
Flexible::make('Content')
->addLayout(ImageAndCaptionLayout::class)
->fullWidth(),

]),

];
}

/**
* Get the cards available for the request.
*
* @return array
*/
public function cards(NovaRequest $request)
{
return [];
}

/**
* Get the filters available for the resource.
*
* @return array
*/
public function filters(NovaRequest $request)
{
return [];
}

/**
* Get the lenses available for the resource.
*
* @return array
*/
public function lenses(NovaRequest $request)
{
return [];
}

/**
* Get the actions available for the resource.
*
* @return array
*/
public function actions(NovaRequest $request)
{
return [];
}
}
19 changes: 19 additions & 0 deletions workbench/app/Nova/Repeater/UserInfoItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Workbench\App\Nova\Repeater;

use Laravel\Nova\Fields\Repeater\Repeatable;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;
use Oneduo\NovaFileManager\FileManager;

class UserInfoItem extends Repeatable
{
public function fields(NovaRequest $request): array
{
return [
Text::make('Card'),
FileManager::make('Image')->wrapper('repeater'),
];
}
}
10 changes: 8 additions & 2 deletions workbench/app/Nova/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace Workbench\App\Nova;

use Illuminate\Http\Request;
use Illuminate\Validation\Rules;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Password;
use Laravel\Nova\Fields\Repeater;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Resource;
use Workbench\App\Nova\Repeater\UserInfoItem;

class User extends Resource
{
Expand All @@ -17,7 +18,7 @@ class User extends Resource
*
* @var string
*/
public static $model = \Illuminate\Foundation\Auth\User::class;
public static $model = \Workbench\App\Models\User::class;

/**
* The single value that should be used to represent the resource when being displayed.
Expand Down Expand Up @@ -59,6 +60,11 @@ public function fields(NovaRequest $request)
->onlyOnForms()
->creationRules('required', Rules\Password::defaults())
->updateRules('nullable', Rules\Password::defaults()),

Repeater::make('Profile')
->repeatables([
UserInfoItem::make(),
]),
];
}

Expand Down
Loading

0 comments on commit 0a1f220

Please sign in to comment.