tylernathanreed / nova-field-manager

Creates a field facade for referencing fields.
MIT License
0 stars 0 forks source link

Relationship Fields Aren't Working #1

Closed HavelTheRawk closed 3 years ago

HavelTheRawk commented 5 years ago

Whenever I try to use the facade to bring in a relationship field, it breaks the page. It looks like it's looking for my class inside of Reedware instead of in my project.

{ "message": "Class 'Reedware\\Project\\Project' not found", "exception": "Symfony\\Component\\Debug\\Exception\\FatalThrowableError", "file": "E:\\Code\\ProjectManager\\ProjectManager\\vendor\\laravel\\nova\\src\\Fields\\BelongsTo.php", "line": 116, "trace": [ { "file": "E:\\Code\\ProjectManager\\ProjectManager\\vendor\\laravel\\nova\\src\\Element.php", "line": 59, "function": "__construct", "class": "Laravel\\Nova\\Fields\\BelongsTo", "type": "->"

tylernathanreed commented 5 years ago

@HavelTheRawk

Could you please add the code that defines your field?

HavelTheRawk commented 5 years ago

Here's the relevant code:

<?php

namespace App\Nova;

use Field;
use Illuminate\Http\Request;
use Laravel\Nova\Http\Requests\NovaRequest;

class Piece extends Resource
{
...
    public function fields(Request $request)
    {
        return [
            Field::id()->sortable(),
            Field::hasMany('Files'),
        ];
    }
...
}

Here's a screenshot of the page when I reach it: image

Here's the telescope output for the exception: Mesage:

Class 'Reedware\Image\Image' not found

Location:

52      * @return void
53      */
54     public function __construct($name, $attribute = null, $resource = null)
55     {
56         parent::__construct($name, $attribute);
57 
58         $resource = $resource ?? ResourceRelationshipGuesser::guessResource($name);
59 
60         $this->resourceClass = $resource;
61         $this->resourceName = $resource::uriKey();
62         $this->hasManyRelationship = $this->attribute;
63     }
64 
65     /**
66      * Determine if the field should be displayed for the given request.
67      *
68      * @param  \Illuminate\Http\Request  $request
69      * @return bool
70      */
71     public function authorize(Request $request)

Stack Trace:

C:\Users\Will\Documents\GitHub\FilingCabinet\vendor\laravel\nova\src\Element.php:46
C:\Users\Will\Documents\GitHub\FilingCabinet\vendor\reedware\nova-field-manager\src\NovaFieldManager.php:49
C:\Users\Will\Documents\GitHub\FilingCabinet\vendor\reedware\nova-field-manager\src\NovaFieldManager.php:62
C:\Users\Will\Documents\GitHub\FilingCabinet\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php:239
C:\Users\Will\Documents\GitHub\FilingCabinet\app\Nova\Piece.php:44
tylernathanreed commented 5 years ago

@HavelTheRawk

This happens because the model class is being instantiated within this package, and it tries to create the new model under the "Reedware" namespace, rather than the "App" namespace.

For now, I would recommend provided all three values to relationship fields, where the resource class is fully qualified:

public function fields(Request $request)
{
    return [
        Field::id()->sortable(),
        Field::hasMany('Files', 'files', File::class),
    ];
}

However, I can agree with you in that this should work out of the box. For now, there's a known workaround, but I'll see if I can find a permanent fix that suits both of our needs.

tylernathanreed commented 3 years ago

@HavelTheRawk This issue has been fixed.