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

Eloquent model to non-eloquent entity relation #104

Open
zlodes opened this issue Jul 12, 2021 · 7 comments
Open

Eloquent model to non-eloquent entity relation #104

zlodes opened this issue Jul 12, 2021 · 7 comments

Comments

@zlodes
Copy link

zlodes commented Jul 12, 2021

Hello!

I want to make a relation from Eloquent model to non-eloquent.
Is it possible?

Thanks.

@lindyhopchris
Copy link
Contributor

Hmmm, not strictly worked this one out yet. If you only need it to appear when serialising to JSON then that's possible by using a Resource class and doing it via the relationships in that. However anything more complex I haven't worked out yet. You'd probably need to ship a your own relationship field type for the schema.

Are you able to investigate using your use case? I'm unlikely to have much time to help out at the moment as I've recently started a new job.

@zlodes
Copy link
Author

zlodes commented Jul 12, 2021

I tried to make my-own ToMany-based relationship class but it's not allowed:

Expecting relationships pipelineSteps to be an Eloquent JSON API to-many relationship.

Ok. I'm gonna try to use Resource class.
Is it possible to add some objects to relationships section of response?

@lindyhopchris
Copy link
Contributor

Yeah so that error is an indication I'm going to have to write a specific field to use for these scenarios.

The resource class is fully documented, so just refer to those docs...

@zlodes
Copy link
Author

zlodes commented Jul 12, 2021

Thanks!

@bbprojectnet
Copy link

I have same issue, I work around it creating the custom action for eloquent resource, for example:

		$server
			->resource('doctors', \App\Http\Controllers\Api\V1\DoctorController::class)
			->only('index', 'show', 'update')
			->relationships(function (Relationships $relationships) {
				$relationships->hasOne('user')->only('related', 'show');
				// $relationships->hasMany('visit-dates')->only('related', 'show');
			})
			->actions(function (ActionRegistrar $actions) {
				$actions->withId()->get('visit-dates', 'readVisitDates'); // non-eloquent pseudo relation
			});

eloquent resource resource:

	public function relationships($request): iterable
	{
		$relationships = iterator_to_array(parent::relationships($request));

		return array_merge($relationships, [
			$this->relation('visit-dates'),
		]);
	}

and the controller:

	public function readVisitDates(AnonymousQuery $query, DoctorSchema $schema, Doctor $doctor): Responsable
	{
		$schema = JsonApi::server()->schemas()->schemaFor('visit-dates');

		$models = $schema
			->repository()
			->queryAll()
			->withRequest($query)
			->withDoctor($doctor)
			->get();

		return DataResponse::make($models);
	}

but also I would be grateful for built-in solution :)

@DellanX
Copy link

DellanX commented Feb 23, 2024

Has there been any movement on this?

I have a calendar model that has events which are stored in an ICalendar format. So I need to be able to pull the non-eloquent data when querying /calendars/{calendar}/events

I'd definitely be willing to PR and experiment, just need to know a little more about the desired direction here.

@lindyhopchris
Copy link
Contributor

Sharing the response I put on the new issue that was opened in the Eloquent repository:
laravel-json-api/eloquent#33 (comment)

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

4 participants