Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nova layout and relations with Layout #73

5 changes: 5 additions & 0 deletions database/factories/PageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
use DrewRoberts\Blog\Models\Page;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Factories\Factory;
use DrewRoberts\Blog\Models\Layout;

class PageFactory extends Factory
{
protected $model = Page::class;

public function definition()
{
$view = $this->faker->numberBetween(0, 1) === 0 ? 'blog::page.base' : 'blog::page.amp';
$layout = Layout::where('view',$view)->first();

$word1 = $this->faker->name;
$word2 = $this->faker->word;

Expand All @@ -29,6 +33,7 @@ public function definition()
'author_id' => randomOrCreate(app('user')),
'creator_id' => randomOrCreate(app('user')),
'updater_id' => randomOrCreate(app('user')),
'layout_id' => $layout->id,
'published_at' => $this->faker->dateTimeBetween($startDate = '-1 years', $endDate = '-1 days', $timezone = null)
];
}
Expand Down
5 changes: 5 additions & 0 deletions database/factories/PostFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
use DrewRoberts\Blog\Models\Post;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Factories\Factory;
use DrewRoberts\Blog\Models\Layout;

class PostFactory extends Factory
{
protected $model = Post::class;

public function definition()
{
$view = $this->faker->numberBetween(0, 1) === 0 ? 'blog::post.base' : 'blog::post.amp';
$layout = Layout::where('view',$view)->first();

$word1 = $this->faker->name;
$word2 = $this->faker->word;

Expand All @@ -30,6 +34,7 @@ public function definition()
'author_id' => randomOrCreate(app('user')),
'creator_id' => randomOrCreate(app('user')),
'updater_id' => randomOrCreate(app('user')),
'layout_id' => $layout->id,
'published_at' => $this->faker->dateTimeBetween($startDate = '-1 years', $endDate = '-1 days', $timezone = null)
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function up()
$table->string('slug')->index();
$table->string('title')->unique();
$table->foreignIdFor(app('layout'))->nullable(); // Will remove nullable and default a basic layout for pages. Allows some pages to have different layout (AMP or regular html & other variations)
$table->boolean('location_based')->default('false');
$table->boolean('location_based')->default(false);
$table->foreignIdFor(app('page'), 'parent_id')->nullable(); // Parent Page
$table->text('content')->nullable(); // Will be written in Markdown.

Expand Down
1 change: 1 addition & 0 deletions src/BlogServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function configureTipoffPackage(TipoffPackage $package): void
\DrewRoberts\Blog\Nova\Post::class,
\DrewRoberts\Blog\Nova\Series::class,
\DrewRoberts\Blog\Nova\Topic::class,
\DrewRoberts\Blog\Nova\Layout::class,
])
->hasWebRoute('web')
->hasViews()
Expand Down
10 changes: 10 additions & 0 deletions src/Models/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,14 @@ class Layout extends BaseModel
use HasCreator;
use HasPackageFactory;
use HasUpdater;

public function posts()
{
return $this->hasMany(app('post'));
}

public function pages()
{
return $this->hasMany(app('page'));
}
}
73 changes: 73 additions & 0 deletions src/Nova/Layout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace DrewRoberts\Blog\Nova;

use Illuminate\Http\Request;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\HasMany;
use Laravel\Nova\Fields\DateTime;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Markdown;
use Laravel\Nova\Fields\Slug;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\Textarea;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Panel;
use Tipoff\Support\Nova\BaseResource;

class Layout extends BaseResource
{
public static $model = \DrewRoberts\Blog\Models\Layout::class;

public static $title = 'name';

public static $search = [
'id',
'name',
];

public static $group = 'Website Layout';

public function fieldsForIndex(NovaRequest $request)
{
return [
ID::make()->sortable(),
Text::make('Name')->sortable()->required(),
Text::make('Layout Type')->sortable()->required(),
Text::make('View')->sortable()->required(),
nova('page') ? HasMany::make('Pages') : null,
nova('post') ? HasMany::make('Posts') : null,
nova('user') ? BelongsTo::make('Author', 'author', nova('user'))->sortable() : null,
];
}

public function fields(Request $request)
{
return [
Text::make('Name')->sortable()->required(),
Text::make('Layout Type')->sortable()->required(),
Text::make('View')->sortable()->required(),
Text::make('Note')->sortable()->nullable(),

new Panel('Info Fields', $this->infoFields()),
new Panel('Data Fields', $this->dataFields()),
];
}

protected function infoFields()
{
return [
nova('user') ? BelongsTo::make('Author', 'author', nova('user'))->sortable() : null,
nova('image') ? BelongsTo::make('Image', 'image', nova('image'))->nullable()->showCreateRelationButton() : null,
];
}

protected function dataFields(): array
{
return array_merge(
parent::dataFields(),
$this->creatorDataFields(),
$this->updaterDataFields(),
);
}
}
1 change: 1 addition & 0 deletions src/Nova/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function fieldsForIndex(NovaRequest $request)
Text::make('Title')->sortable(),
nova('page') ? BelongsTo::make('Parent', 'parent', nova('page'))->sortable() : null,
nova('user') ? BelongsTo::make('Author', 'author', nova('user'))->sortable() : null,
nova('layout') ? BelongsTo::make('Layout', 'layout', nova('layout'))->nullable() : null,
DateTime::make('Published', 'published_at')->format('YYYY-MM-DD')->sortable(),
];
}
Expand Down
1 change: 1 addition & 0 deletions src/Nova/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function fieldsForIndex(NovaRequest $request)
Text::make('Title')->sortable(),
nova('series') ? BelongsTo::make('Series', 'series', nova('series'))->sortable() : null,
nova('user') ? BelongsTo::make('Author', 'author', nova('user'))->sortable() : null,
nova('layout') ? BelongsTo::make('Layout', 'layout', nova('layout'))->nullable() : null,
DateTime::make('Published', 'published_at')->format('YYYY-MM-DD')->sortable(),
];
}
Expand Down
25 changes: 25 additions & 0 deletions tests/Unit/Models/LayoutTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace DrewRoberts\Blog\Tests\Unit\Models;

use DrewRoberts\Blog\Models\Layout;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Carbon;
use DrewRoberts\Blog\Tests\TestCase;
use Tipoff\Authorization\Models\User;

class LayoutTest extends TestCase
{
use RefreshDatabase,
WithFaker;

/** @test */
public function create_layout()
{
$name = $this->faker->name;
$layout = Layout::factory()->create(['name' => $name]);

$this->assertEquals($name, $layout->name);
}
}