1

The plugin I've created has two tables with these structures :

CREATE TABLE notifications (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `created_at` TIMESTAMP NULL DEFAULT NULL,
    `updated_at` TIMESTAMP NULL DEFAULT NULL,
    `sent_at` DATETIME NULL DEFAULT NULL,
    `title` VARCHAR(191) NOT NULL COLLATE 'utf8mb4_unicode_ci',
    `message` TEXT NOT NULL COLLATE 'utf8mb4_unicode_ci',
    `photo` VARCHAR(191) NOT NULL COLLATE 'utf8mb4_unicode_ci',
    `icon` VARCHAR(191) NOT NULL COLLATE 'utf8mb4_unicode_ci'
)

CREATE TABLE notification_recipients(
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `created_at` TIMESTAMP NULL DEFAULT NULL,
    `updated_at` TIMESTAMP NULL DEFAULT NULL,
    `notification_id` INT(11) NOT NULL,
    `user_id` INT(11) NOT NULL,
    `seen` TINYINT(1) NOT NULL,
    `archived` TINYINT(1) NOT NULL
)

I can't figure out how to create a relation to show a backend select field where I can select users to get the notification, what should be done in the model.

I will later process the form in the controller in order to create the notification_recipients line to every user selected.

After reading and watching the tutorial about relations, I've come to understand I might need a pivot table.

But I need the notification_recipients table in order to process the notification later to show custom queries in frontend (seen and archived fields will determine the visibility in frontend).

How can I do that ? I don't understand the relationship I'm trying to build, shouw I have a third table ?

Thank you, best regards.

Update (solution found) :

I added a Checkbox List named "users". According to the docs of OctoberCMS, you can add options from the model to a Radio field, and Checkbox Lists work the same way.

For now, I got it working by doing this in the Model used for the form:

use RainLab\User\Models\User;
...
public function getUsersOptions($value, $formData)
{
    $users = User::all();
    $mapped = $users->mapWithKeys(function ($item) {
        return [$item['id'] => $item['name']];
    });
    return $mapped;
}

It worked great and I'm getting all users with their ids.

Mr.Miel
  • 159
  • 1
  • 3
  • 13
  • 1
    might this link https://laravel.com/docs/5.8/eloquent-relationships#many-to-many at session "Retrieving Intermediate Table Columns" could help – Joao Polo May 25 '19 at 22:24
  • 1
    Thank you, this helped understand the relationship I have to build between Notifications and NotificationRecipient. I also added an answer to myself in the original post, I found an internal solution related to OctoberCMS to populate the list without making relations, which is intended as I only need to get Users – Mr.Miel May 25 '19 at 23:00
  • Hm, now I understood what was you need. Yes, you need to select separated from the pivot... I use to create specific endpoints for each select content – Joao Polo May 26 '19 at 00:16

0 Answers0