Skip to content

Commit

Permalink
append ->nullable() to nullable relationships - closes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
Naoray committed Apr 19, 2020
1 parent f53dbce commit 403439a
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/Tasks/AddRelationshipFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

use Closure;
use Blueprint\Models\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

class AddRelationshipFields
{
use InteractWithRelationships;

const INDENT = ' ';

/** @var Model */
Expand Down Expand Up @@ -46,11 +49,17 @@ public function handle(array $data, Closure $next)

$fields .= self::INDENT . $fieldType . "::make('" . $label . "'";

if ($label !== $class && $label !== Str::plural($class)) {
if ($this->classNameNotGuessable($label, $class)) {
$fields .= ", '" . $methodName . "', " . $class . '::class';
}

$fields .= '),' . PHP_EOL;
$fields .= ')';

if ($this->isNullable($reference)) {
$fields .= '->nullable()';
}

$fields .= ',' . PHP_EOL;
}

$fields .= PHP_EOL;
Expand All @@ -74,6 +83,25 @@ private function buildMethodName(string $name, string $type)
: $name;
}

private function classNameNotGuessable($label, $class): bool
{
return $label !== $class
&& $label !== Str::plural($class);
}

private function isNullable($relation): bool
{
$relationColumnName = $this->relationshipIdentifiers($this->model->columns())
->filter(function ($relationReference, $columnName) use ($relation) {
return in_array($relationReference, Arr::get($this->model->relationships(), 'belongsTo', []))
&& $columnName === $relation;
})
->first();

return !is_null($relationColumnName)
&& in_array('nullable', $this->model->columns()[$relationColumnName]->modifiers());
}

private function fieldType(string $dataType): string
{
static $fieldTypes = [
Expand Down
1 change: 1 addition & 0 deletions tests/NovaGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public function novaTreeDataProvider()
['definitions/with-timezones.bp', 'app/Nova/Comment.php', 'nova/with-timezones.php'],
['definitions/relationships.bp', 'app/Nova/Comment.php', 'nova/relationships.php'],
['definitions/unconventional.bp', 'app/Nova/Team.php', 'nova/unconventional.php'],
['definitions/nullable-relationships.bp', 'app/Nova/Subscription.php', 'nova/nullable-relationships.php'],
/*
* @todo work on this
*/
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/definitions/nullable-relationships.bp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
models:
Subscription:
user_id: id nullable
item_id: id:product
97 changes: 97 additions & 0 deletions tests/fixtures/nova/nullable-relationships.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace App\Nova;

use Laravel\Nova\Fields\ID;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\DateTime;
use Laravel\Nova\Fields\BelongsTo;

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

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

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

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

BelongsTo::make('User')->nullable(),
BelongsTo::make('Item', 'item', Product::class),

DateTime::make('Created at'),
DateTime::make('Updated at'),
];
}

/**
* Get the cards available for the request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function cards(Request $request)
{
return [];
}

/**
* Get the filters available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function filters(Request $request)
{
return [];
}

/**
* Get the lenses available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function lenses(Request $request)
{
return [];
}

/**
* Get the actions available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function actions(Request $request)
{
return [];
}
}

0 comments on commit 403439a

Please sign in to comment.