Replies: 9 comments 6 replies
-
@crynobone can you please tell me what info is needed? |
Beta Was this translation helpful? Give feedback.
-
All bug report should come with full reproducing code, not just code snippets. |
Beta Was this translation helpful? Give feedback.
-
You can just copy the code from the Laravel documentation, but here it is... class CreateMorphsTable extends Migration
{
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->text('name');
$table->timestamps();
});
Schema::create('videos', function (Blueprint $table) {
$table->id();
$table->text('name');
$table->timestamps();
});
Schema::create('tags', function (Blueprint $table) {
$table->id();
$table->text('name');
$table->timestamps();
});
Schema::create('taggables', function (Blueprint $table) {
$table->id();
$table->foreignId('tag_id');
$table->morphs('taggable');
$table->timestamps();
});
}
} Models class Video extends Model
{
public function tags()
{
return $this->morphToMany(Tag::class, 'taggable');
}
}
class Post extends Model
{
public function tags()
{
ray(request()->url(), $this)->label(__FUNCTION__);
return $this->morphToMany(Tag::class, 'taggable');
}
}
class Tag extends Model
{
public function posts()
{
return $this->morphedByMany(Post::class, 'taggable');
}
public function videos()
{
return $this->morphedByMany(Video::class, 'taggable');
}
} Nova Models class Video extends Resource
{
public function fields(Request $request)
{
return [
ID::make(__('ID'), 'id')->sortable(),
Text::make('Name'),
MorphToMany::make('Tags'),
];
}
}
class Post extends Resource
{
public function fields(Request $request)
{
return [
ID::make(__('ID'), 'id')->sortable(),
Text::make('Name'),
MorphToMany::make('Tags'),
];
}
}
class Tag extends Resource
{
public function fields(Request $request)
{
return [
ID::make(__('ID'), 'id')->sortable(),
Text::make('Name'),
];
}
} |
Beta Was this translation helpful? Give feedback.
-
I know it's doing something it shouldn't because if you access |
Beta Was this translation helpful? Give feedback.
-
Calling blank model occurred in some API request and that is to be expected depending on what information needed for that particular API request. I don't think there any bug here, as that is expected behaviour. |
Beta Was this translation helpful? Give feedback.
-
@crynobone If you do They are both accessing the same data, what ever is happening in that API endpoint I wouldn't call "to be expected" as that doesn't occur when doing it outside of Nova. What is happening in the endpoint that is causing this? |
Beta Was this translation helpful? Give feedback.
-
above are only your expectations, what we do is only |
Beta Was this translation helpful? Give feedback.
-
Depending on the setup, Nova sometimes spins up a resource that doesn't have a Model instance hydrated. For example, if the field is set up to not defer the query (e.g a What we need you to provide is the full reproducing code, along with descriptions of the problems you're facing. "This is causing me problems and doesn't seem right." is a little vague. Will be glad to turn this back into an issue once we get that. Thanks! |
Beta Was this translation helpful? Give feedback.
-
/**
* Get all of the tags for the post.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
*/
public function tags()
{
ray(request()->fullUrl(), $this, collect(debug_backtrace())->transform(function ($stack) {
if (!isset($stack['file']) || !isset($stack['line'])) {
return null;
}
return $stack['file'].':'.$stack['line'];
})->all())->label(__FUNCTION__);
return $this->morphToMany(Tag::class, 'taggable')->withPivot('notes');
} Code above added to Line 707 on |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Description:
If you add a Ray debug to the relationship method, the API request is returning blank models as shown below when accessing a Post resource.
In my example this Post has 3 Tags, and it seem to be outputting 3 blank Post models in the debug output.

This is causing me problems and doesn't seem right.
Why is this happening and is there a way to prevent it?
Detailed steps to reproduce the issue on a fresh Nova installation:
1.) Follow this structure for creating Laravel & Nova Models:
https://laravel.com/docs/8.x/eloquent-relationships#many-to-many-polymorphic-relations
2.) Create / Seed the Tags and Posts + Videos
3.) Navigate to a Post
Beta Was this translation helpful? Give feedback.
All reactions