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

Polymorphic Relations #26

Open
adrianpaiva1 opened this issue Feb 15, 2020 · 13 comments
Open

Polymorphic Relations #26

adrianpaiva1 opened this issue Feb 15, 2020 · 13 comments
Labels
enhancement New feature or request

Comments

@adrianpaiva1
Copy link

Hi,

love the package, but I cant seem to get polymorphic relations working from within the JSON column.

For example, if I have a User model with a JSON column named 'custom_fields' which is an array of objects. Would it be possible to store something like

[ { 'relationable_id': 12, 'relationable_type: 'App/Role' }, { 'relationable_id': 13, 'relationable_type: 'App/Permission' } ]

Can you provide an example of doing many to many polymorphic relations?

@staudenmeir
Copy link
Owner

This is not supported yet, but I'll look into it.

@staudenmeir staudenmeir added the enhancement New feature or request label Feb 15, 2020
@staudenmeir
Copy link
Owner

Can you provide details about your use case?

@zikraauliya
Copy link

In my use case I have all images in media table where I can refer to it from any model with morphOne or morphMany. Initialy I want to set user's profile picture in json column in users table, but since this isn't supported yet I just set the user's profile picture in its own column. but I think it will be nice to have this morphOne or morphMany relationship in json column, since by default every user get default gravatar (or anything) profile picture and not really required to change the profile picture, so we don't get lots of image column with null values.

Thanks by the way, this really is a great package.

@staudenmeir
Copy link
Owner

@zickcrow Thanks for the use case. Can you share the migration/structure of your media table?

@zikraauliya
Copy link

zikraauliya commented Mar 29, 2020

CREATE TABLE media (
id bigint(20) UNSIGNED NOT NULL,
model_type varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
model_id bigint(20) UNSIGNED NOT NULL,
uuid char(36) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
collection_name varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
name varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
file_name varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
mime_type varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
disk varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
conversions_disk varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
size bigint(20) UNSIGNED NOT NULL,
manipulations json NOT NULL,
custom_properties json NOT NULL,
responsive_images json NOT NULL,
order_column int(10) UNSIGNED DEFAULT NULL,
created_at timestamp NULL DEFAULT NULL,
updated_at timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

FYI,I use https://docs.spatie.be/laravel-medialibrary/v8/introduction/ which generates this table

@staudenmeir
Copy link
Owner

@zickcrow It's actually already possible to use a JSON column here.

Regarding your setup: You are connecting user and image with a column in the users table, right (not with media.model_type and model_id)? Or am I misunderstanding this?

@zikraauliya
Copy link

Yes and no, its my mistake. I think you can close this now. Thanks

@designbyjr
Copy link

@staudenmeir PHP actually encodes arrays as JSON object arrays and since this is for laravel, it would be useful to have this.
For example if I have a PHP array and then want to encode it as JSON then thats where i have problems as stated above. The column I have is in JSON.

@staudenmeir
Copy link
Owner

PHP actually encodes arrays as JSON object arrays

What do you mean by that?

What do your tables and JSON values look like? What relationship(s) have you tried?

@asandri-mmo
Copy link

Is morphTo and morphToMany supported?

@staudenmeir
Copy link
Owner

@asandri-mmo What do your tables look like?

@asandri-mmo
Copy link

asandri-mmo commented Sep 15, 2020

@asandri-mmo What do your tables look like?

@staudenmeir

Schema::create('candidates', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});

Schema::create('jobs', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->timestamps();
});

Schema::create('applications', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->timestamps();
});

Schema::create('messages', function (Blueprint $table) {
$table->increments('id');
$table->mediumText('content');
$table->timestamp('scheduled_at')->nullable();
$table->json('options'); // {'messageable_type': '', 'messageable_ids': []}
$table->timestamps();
});

Messageable Types are candidates, jobs, applications
Here can I use morphTo in case if messages table has options->messageable_id
Or can I use morphToMany in case if messages table has options->messageable_ids

Please help

@staudenmeir
Copy link
Owner

@asandri-mmo This case is not supported yet.

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

No branches or pull requests

5 participants