Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moprh + include = method not found #558

Closed
rekrios opened this issue Sep 28, 2020 · 4 comments
Closed

Moprh + include = method not found #558

rekrios opened this issue Sep 28, 2020 · 4 comments

Comments

@rekrios
Copy link
Contributor

rekrios commented Sep 28, 2020

Hi.
Is there any easy way to include resources after polymorp(MorphTo) relationship ?
Problem: after polymorph relationship different resources have different relationships and you will get fatal with 'method/relationship not found'.
I can make eager loading for such resources via

  $morphTo->morphWith([
    Model1::class => [
      'relation1.relation11'
    ],
    Model2::class => [
      'relation2',
    ],
  ]);

But can't say the same for includes.
Maybe there is the way to show them even without them in $_GET path ?

@rekrios rekrios changed the title Moprh + include Moprh + include = method not found Sep 28, 2020
@rekrios
Copy link
Contributor Author

rekrios commented Sep 28, 2020

Found way #1:
Without $_GET's included path, you can set includes through schema's

    public function getIncludePaths()
    {
        return ['relation1.relation11', 'relation2.relation22'];
    }

But not convenient for all cases. Maybe someone know's better way

@lindyhopchris
Copy link
Member

Yeah, polymorphic relationships present a number of problems that I don't think I've ever fully cracked. Think they need some work in future versions of this package to get them working better. Sorry not to be able to provide an immediate fix.

@bbprojectnet
Copy link

bbprojectnet commented Dec 4, 2020

I have similar case in my project. I solved this by defining dummy relations in model to omit error:

trait HasNoneRelations
{
	public function __call($method, $parameters)
	{
		if (in_array($method, $this->getNoneRelations())) {
			return $this->belongsToNone();
		}

		return parent::__call($method, $parameters);
	}

	public function getNoneRelations(): array
	{
		return $this->noneRelations ?? [];
	}

	protected function belongsToNone(): BelongsTo
	{
		return new BelongsTo($this->newQuery(), $this, '', '', '');
	}
}

and then, in model:

class Game extends Model
{
	use HasNoneRelations;

	protected $noneRelations = [
		'attachment',
		'attachments',
		'reads',
		'other-non-existing-relation',
	];
}

now you can include through morph relationship :)

I am curious about a more elegant solution.

@lindyhopchris
Copy link
Member

Closing this in favour of polymorphic relations in the new package - laravel-json-api/laravel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants