Skip to content

Commit

Permalink
Beta one
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel de Wit committed Dec 6, 2018
0 parents commit b70506f
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 0 deletions.
43 changes: 43 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "daniel-de-wit/nova-single-record-resource",
"description": "Laravel Nova Single Record Resource. Shows a link in the navigation bar directly to the only record of the resource",
"keywords": [
"laravel",
"Nova"
],
"homepage": "https://github.com/daniel-de-wit",
"license": "MIT",
"authors": [
{
"name": "Daniël de Wit",
"email": "[email protected]",
"homepage": "https://github.com/daniel-de-wit",
"role": "Developer"
}
],
"require": {
"php": ">=7.0.0"
},
"require-dev": {
},
"autoload": {
"psr-4": {
"DanielDeWit\\NovaSingleRecordResource\\": "src"
}
},
"autoload-dev": {
"psr-4": {
}
},
"scripts": {
},
"extra": {
"laravel": {
"providers": [
"DanielDeWit\\NovaSingleRecordResource\\NovaSingleRecordResourceServiceProvider"
]
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
57 changes: 57 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Laravel Nova: Single Record Resource

Adds the ability to create a navigation link directly to the detail page of a resource.
Useful for models that will contain only a single record.

## Prerequisites
- [Laravel](https://laravel.com/)
- [Laravel Nova](https://nova.laravel.com/)

## Installation

```
$ composer require daniel-de-wit/nova-single-record-resource
```

Modify `app/Nova/Resource.php` to implement `SingleRecordResourceInterface` and the `SingleRecord` trait:

```php
<?php

namespace App\Nova;

use DanielDeWit\NovaSingleRecordResource\Contracts\SingleRecordResourceInterface;
use DanielDeWit\NovaSingleRecordResource\Traits\SupportSingleRecordNavigationLinks;
use Laravel\Nova\Resource as NovaResource;

abstract class Resource extends NovaResource implements SingleRecordResourceInterface
{
use SupportSingleRecordNavigationLinks;

...
}
```

## Usage

Place the following method on models that have only a single record.

```php
class MyResource extends Resource
{
public static function singleRecord(): bool
{
return true;
}
}
```

Publish the view template:
```
php artisan vendor:publish --force --provider="DanielDeWit\NovaSingleRecordResource\Providers\NovaSingleRecordResourceServiceProvider"
```

## Important

Laravel Nova currently does not support an integration without overriding the navigation blade template.
Therefore a vendor publish is required with the force flag.
49 changes: 49 additions & 0 deletions resources/views/resources/navigation.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
@if (count(Nova::availableResources(request())))
<h3 class="flex items-center font-normal text-white mb-6 text-base no-underline">
<svg class="sidebar-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
<path fill="var(--sidebar-icon)" d="M3 1h4c1.1045695 0 2 .8954305 2 2v4c0 1.1045695-.8954305 2-2 2H3c-1.1045695 0-2-.8954305-2-2V3c0-1.1045695.8954305-2 2-2zm0 2v4h4V3H3zm10-2h4c1.1045695 0 2 .8954305 2 2v4c0 1.1045695-.8954305 2-2 2h-4c-1.1045695 0-2-.8954305-2-2V3c0-1.1045695.8954305-2 2-2zm0 2v4h4V3h-4zM3 11h4c1.1045695 0 2 .8954305 2 2v4c0 1.1045695-.8954305 2-2 2H3c-1.1045695 0-2-.8954305-2-2v-4c0-1.1045695.8954305-2 2-2zm0 2v4h4v-4H3zm10-2h4c1.1045695 0 2 .8954305 2 2v4c0 1.1045695-.8954305 2-2 2h-4c-1.1045695 0-2-.8954305-2-2v-4c0-1.1045695.8954305-2 2-2zm0 2v4h4v-4h-4z"
/>
</svg>
<span class="sidebar-label">{{ __('Resources') }}</span>
</h3>

@foreach(Nova::groupedResources(request()) as $group => $resources)
@if (count($resources) > 0)
@if (count(Nova::groups(request())) > 1)
<h4 class="ml-8 mb-4 text-xs text-white-50% uppercase tracking-wide">{{ $group }}</h4>
@endif

<ul class="list-reset mb-8">
@foreach($resources as $resource)
@if (! $resource::$displayInNavigation)
@continue
@endif

<li class="leading-tight mb-4 ml-8 text-sm">
@if ($resource::singleRecord())
<router-link :to="{
name: 'detail',
params: {
resourceName: '{{ $resource::uriKey() }}',
resourceId: {{ $resource::firstRecordId() }}
}
}" class="text-white text-justify no-underline dim">
{{ $resource::label() }}
</router-link>
@else
<router-link :to="{
name: 'index',
params: {
resourceName: '{{ $resource::uriKey() }}'
}
}" class="text-white text-justify no-underline dim">
{{ $resource::label() }}
</router-link>
@endif
</li>
@endforeach
</ul>
@endif
@endforeach
@endif

10 changes: 10 additions & 0 deletions src/Contracts/SingleRecordResourceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace DanielDeWit\NovaSingleRecordResource\Contracts;

interface SingleRecordResourceInterface
{
public static function singleRecord(): bool;

public static function firstRecordId(): int;
}
25 changes: 25 additions & 0 deletions src/Providers/NovaSingleRecordResourceServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace DanielDeWit\NovaSingleRecordResource\Providers;

use Illuminate\Support\ServiceProvider;

class NovaSingleRecordResourceServiceProvider extends ServiceProvider
{
public function boot(): void
{
$this->publishAssets();
}

/**
* @return $this
*/
protected function publishAssets()
{
$this->publishes([
__DIR__.'/../../resources/views/resources' => resource_path('views/vendor/nova/resources'),
], 'nova-views');

return $this;
}
}
16 changes: 16 additions & 0 deletions src/Traits/SupportSingleRecordNavigationLinks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace DanielDeWit\NovaSingleRecordResource\Traits;

trait SupportSingleRecordNavigationLinks
{
public static function singleRecord(): bool
{
return false;
}

public static function firstRecordId(): int
{
return 1;
}
}

0 comments on commit b70506f

Please sign in to comment.