PHP code example of nevadskiy / nova-collection-field
1. Go to this page and download the library: Download nevadskiy/nova-collection-field 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/ */
nevadskiy / nova-collection-field example snippets
namespace App\Nova;
use App\Models\FaqSection as FaqSectionModel;
use App\Nova\Resource;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;
use Nevadskiy\Nova\Collection\HasManyCollection;
class FaqSection extends Resource
{
public static string $model = FaqSectionModel::class;
public static $title = 'heading';
public static $search = [
'heading',
];
public function fields(NovaRequest $request): array
{
return [
ID::make(),
Text::make('Heading'),
HasManyCollection::make('Questions', 'items', FaqItem::class)
->sortBy('position')
->stacked()
->fullWidth()
];
}
}
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class FaqSection extends Model
{
public function items(): HasMany
{
return $this->hasMany(FaqItem::class);
}
}
namespace App\Nova;
use App\Models\Page as PageModel;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;
use Nevadskiy\Nova\Collection\MorphToManyCollection;
class Page extends Resource
{
public static string $model = PageModel::class;
public function fields(NovaRequest $request): array
{
return [
ID::make(),
Text::make('Title'),
MorphToManyCollection::make('Components')
->resources([
'heroSections' => HeroSection::class,
'demoSections' => DemoSection::class,
'faqSections' => FaqSection::class,
])
->sortBy('position')
->attachable()
->collapsable()
->stacked()
->fullWidth(),
];
}
}
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
class Page extends Model
{
public function heroSections(): MorphToMany
{
return $this->morphedByMany(HeroSection::class, 'page_component')
->withPivot('position');
}
public function demoSections(): MorphToMany
{
return $this->morphedByMany(DemoSection::class, 'page_component')
->withPivot('position');
}
public function faqSections(): MorphToMany
{
return $this->morphedByMany(FaqSection::class, 'page_component')
->withPivot('position');
}
}
namespace App\Nova;
use App\Models\Page as PageModel;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;
use Nevadskiy\Nova\Collection\MorphToManyCollection;
class Page extends Resource
{
public static string $model = PageModel::class;
public function fields(NovaRequest $request): array
{
return [
ID::make(),
Text::make('Title'),
ManyToMorphCollection::make('Components')
->resources([
HeroSection::class,
DemoSection::class,
FaqSection::class,
])
->sortBy('position')
->attachable()
->collapsable()
->stacked()
->fullWidth(),
];
}
}
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Nevadskiy\ManyToMorph\HasManyToMorph;
use Nevadskiy\ManyToMorph\ManyToMorph;
class Page extends Model
{
use HasManyToMorph;
public function components(): ManyToMorph
{
return $this->manyToMorph('page_component');
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.