Skip to content

Commit

Permalink
Use of Laravel Linkable
Browse files Browse the repository at this point in the history
  • Loading branch information
felixgilles committed Aug 6, 2024
1 parent cc364ce commit 9d2a0bd
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 137 deletions.
5 changes: 1 addition & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"php": "^8.2",
"illuminate/support": "^10.0|^11.0",
"laravel/nova": "^4.0",
"novius/laravel-linkable": "^1.0",
"novius/laravel-meta": "^1.0",
"novius/laravel-nova-field-preview": "^2.0",
"novius/laravel-nova-publishable": "^3.0",
Expand Down Expand Up @@ -67,10 +68,6 @@
{
"type": "composer",
"url": "https://nova.laravel.com"
},
{
"type": "vcs",
"url": "[email protected]:novius/laravel-meta.git"
}
],
"config": {
Expand Down
27 changes: 21 additions & 6 deletions src/LaravelNovaNewsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;
use Laravel\Nova\Nova;
use Novius\LaravelLinkable\Facades\Linkable;
use Novius\LaravelNovaNews\Console\FrontControllerCommand;
use Novius\LaravelNovaNews\Models\NewsCategory;
use Novius\LaravelNovaNews\Models\NewsPost;
Expand All @@ -24,12 +25,26 @@ public function register(): void
*/
public function boot(): void
{
// Load Nova resources
Nova::resources(array_filter([
NovaNews::getPostResource(),
NovaNews::getCategoryResource(),
NovaNews::getTagResource(),
]));
$this->app->booted(function () {
// Load Nova resources
Nova::resources(array_filter([
NovaNews::getPostResource(),
NovaNews::getCategoryResource(),
NovaNews::getTagResource(),
]));
});

$this->app->booted(function () {
Linkable::addModels(array_filter([
NovaNews::getPostModel(),
NovaNews::getCategoryModel(),
NovaNews::getTagModel(),
]));
Linkable::addRoutes(array_flip(array_filter([
trans('laravel-nova-news::crud-post.resource_label') => config('laravel-nova-news.front_routes_name.posts'),
trans('laravel-nova-news::crud-category.resource_label') => config('laravel-nova-news.front_routes_name.categories'),
])));
});

$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
$this->loadTranslationsFrom(__DIR__.'/../lang', 'laravel-nova-news');
Expand Down
105 changes: 0 additions & 105 deletions src/Models/ModelWithUrl.php

This file was deleted.

38 changes: 31 additions & 7 deletions src/Models/NewsCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;
use Novius\LaravelLinkable\Configs\LinkableConfig;
use Novius\LaravelLinkable\Traits\Linkable;
use Novius\LaravelMeta\Enums\IndexFollow;
use Novius\LaravelMeta\MetaModelConfig;
use Novius\LaravelMeta\Traits\HasMeta;
Expand Down Expand Up @@ -47,11 +50,12 @@
*
* @mixin Eloquent
*/
class NewsCategory extends ModelWithUrl
class NewsCategory extends Model
{
use HasFactory;
use HasMeta;
use HasSlug;
use Linkable;
use SoftDeletes;
use Translatable;

Expand All @@ -77,6 +81,11 @@ protected static function booted(): void
});
}

public function getRouteKeyName(): string
{
return 'slug';
}

public function getSlugOptions(): SlugOptions
{
return SlugOptions::create()
Expand All @@ -96,14 +105,29 @@ public function getMetaConfig(): MetaModelConfig
return $this->metaConfig;
}

public function getFrontRouteName(): ?string
{
return config('laravel-nova-news.front_routes_name.category');
}
protected ?LinkableConfig $_linkableConfig;

public function getFrontRouteParameter(): ?string
public function linkableConfig(): ?LinkableConfig
{
return config('laravel-nova-news.front_routes_parameters.category');
$route = config('laravel-nova-news.front_routes_name.post');
$routeParameterName = config('laravel-nova-news.front_routes_parameters.post');
if (empty($routeParameterName) && empty($route)) {
return null;
}

if (! isset($this->_linkableConfig)) {
$this->_linkableConfig = new LinkableConfig(
routeName: $route,
routeParameterName: $routeParameterName,
optionLabel: 'name',
optionGroup: trans('laravel-nova-news::crud-category.resource_label'),
resolveQuery: function (Builder|NewsCategory $query) {
$query->where('locale', app()->currentLocale());
},
);
}

return $this->_linkableConfig;
}

public function localParent()
Expand Down
48 changes: 38 additions & 10 deletions src/Models/NewsPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use Novius\LaravelLinkable\Configs\LinkableConfig;
use Novius\LaravelLinkable\Traits\Linkable;
use Novius\LaravelMeta\Enums\IndexFollow;
use Novius\LaravelMeta\MetaModelConfig;
use Novius\LaravelMeta\Traits\HasMeta;
Expand Down Expand Up @@ -71,11 +74,12 @@
*
* @mixin Eloquent
*/
class NewsPost extends ModelWithUrl
class NewsPost extends Model
{
use HasFactory;
use HasMeta;
use HasSlug;
use Linkable;
use Publishable;
use SoftDeletes;
use Translatable;
Expand Down Expand Up @@ -103,19 +107,14 @@ protected static function booted(): void
});
}

public function isFeatured(): bool
{
return $this->featured;
}

public function getFrontRouteName(): ?string
public function getRouteKeyName(): string
{
return config('laravel-nova-news.front_routes_name.post');
return 'slug';
}

public function getFrontRouteParameter(): ?string
public function isFeatured(): bool
{
return config('laravel-nova-news.front_routes_parameters.post');
return $this->featured;
}

public function getSlugOptions(): SlugOptions
Expand All @@ -139,6 +138,35 @@ public function getMetaConfig(): MetaModelConfig
return $this->metaConfig;
}

protected ?LinkableConfig $_linkableConfig;

public function linkableConfig(): ?LinkableConfig
{
$route = config('laravel-nova-news.front_routes_name.post');
$routeParameterName = config('laravel-nova-news.front_routes_parameters.post');
if (empty($routeParameterName) && empty($route)) {
return null;
}

if (! isset($this->_linkableConfig)) {
$this->_linkableConfig = new LinkableConfig(
routeName: $route,
routeParameterName: $routeParameterName,
optionLabel: 'title',
optionGroup: trans('laravel-nova-news::crud-post.resource_label'),
resolveQuery: function (Builder|NewsPost $query) {
$query->where('locale', app()->currentLocale());
},
resolveNotPreviewQuery: function (Builder|NewsPost $query) {
$query->published();
},
previewTokenField: 'preview_token'
);
}

return $this->_linkableConfig;
}

public function localParent(): HasOne
{
return $this->hasOne(static::class, 'id', 'locale_parent_id');
Expand Down
35 changes: 30 additions & 5 deletions src/Models/NewsTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

namespace Novius\LaravelNovaNews\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;
use Novius\LaravelLinkable\Configs\LinkableConfig;
use Novius\LaravelLinkable\Traits\Linkable;
use Novius\LaravelNovaNews\Database\Factories\NewsTagFactory;
use Novius\LaravelNovaNews\NovaNews;
use Novius\LaravelTranslatable\Traits\Translatable;
Expand All @@ -24,10 +28,11 @@
* @property Carbon|null $updated_at
* @property Carbon|null $deleted_at
*/
class NewsTag extends ModelWithUrl
class NewsTag extends Model
{
use HasFactory;
use HasSlug;
use Linkable;
use SoftDeletes;
use Translatable;

Expand All @@ -53,14 +58,34 @@ protected static function booted()
});
}

public function getFrontRouteName(): ?string
public function getRouteKeyName(): string
{
return config('laravel-nova-news.front_routes_name.tag');
return 'slug';
}

public function getFrontRouteParameter(): ?string
protected ?LinkableConfig $_linkableConfig;

public function linkableConfig(): ?LinkableConfig
{
return config('laravel-nova-news.front_routes_parameters.tag');
$route = config('laravel-nova-news.front_routes_name.tag');
$routeParameterName = config('laravel-nova-news.front_routes_parameters.tag');
if (empty($routeParameterName) && empty($route)) {
return null;
}

if (! isset($this->_linkableConfig)) {
$this->_linkableConfig = new LinkableConfig(
routeName: $route,
routeParameterName: $routeParameterName,
optionLabel: 'name',
optionGroup: trans('laravel-nova-news::crud-tag.resource_label'),
resolveQuery: function (Builder|NewsCategory $query) {
$query->where('locale', app()->currentLocale());
},
);
}

return $this->_linkableConfig;
}

public function getSlugOptions(): SlugOptions
Expand Down

0 comments on commit 9d2a0bd

Please sign in to comment.