staudenmeir / eloquent-json-relations

Laravel Eloquent relationships with JSON keys
MIT License
982 stars 63 forks source link

Polymorphic Relations #26

Open adrianpaiva1 opened 4 years ago

adrianpaiva1 commented 4 years ago

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 commented 4 years ago

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

staudenmeir commented 4 years ago

Can you provide details about your use case?

zikraauliya commented 4 years ago

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 commented 4 years ago

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

zikraauliya commented 4 years ago

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 commented 4 years ago

@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 commented 4 years ago

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

designbyjr commented 4 years ago

@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 commented 4 years ago

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 commented 4 years ago

Is morphTo and morphToMany supported?

staudenmeir commented 4 years ago

@asandri-mmo What do your tables look like?

asandri-mmo commented 3 years ago

@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 commented 3 years ago

@asandri-mmo This case is not supported yet.