How can send a Message Notification to all my user using a select option?
Hello everyone, I have a form configured to send internal messages in my system, I have it programmed using a select from the form in the image like this:
And this is my migration table:
Now I want to add an option in the select that allows me to send the notification message to all my tenant users within the system, how could I do this, by chance could you help me with some code example and/or what do you suggest I improve on? this part?
Thank you so much
public static function form(Form $form): Form
{
$tenant = Filament::getTenant();
return $form
->schema([
Forms\Components\TextInput::make('name')
->label('Asunto')
->required(),
Forms\Components\Select::make('email')
->label(__('Seleccione el alumno (Cliente)'))
->options(function () use ($tenant) {
// Obtener los riders por el company_id del tenant
$riders = Rider::whereHas('companies', function ($query) use ($tenant) {
$query->where('companies.id', $tenant->id);
})->orderBy('name')->get();
// Construir el array de opciones para el Select
$options = $riders->pluck('name', 'email')->toArray();
return $options;
})
->required(),
Forms\Components\RichEditor::make('reason')
->label('Contenido del mensaje')
->required()
]);
}
public static function form(Form $form): Form
{
$tenant = Filament::getTenant();
return $form
->schema([
Forms\Components\TextInput::make('name')
->label('Asunto')
->required(),
Forms\Components\Select::make('email')
->label(__('Seleccione el alumno (Cliente)'))
->options(function () use ($tenant) {
// Obtener los riders por el company_id del tenant
$riders = Rider::whereHas('companies', function ($query) use ($tenant) {
$query->where('companies.id', $tenant->id);
})->orderBy('name')->get();
// Construir el array de opciones para el Select
$options = $riders->pluck('name', 'email')->toArray();
return $options;
})
->required(),
Forms\Components\RichEditor::make('reason')
->label('Contenido del mensaje')
->required()
]);
}
public function up(): void
{
Schema::create('messages', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email');
$table->string('reason');
$table->string('status')->default(false);
$table->foreignId('company_id')->constrained();
$table->timestamps();
});
}
public function up(): void
{
Schema::create('messages', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email');
$table->string('reason');
$table->string('status')->default(false);
$table->foreignId('company_id')->constrained();
$table->timestamps();
});
}
2 Replies
You can do somethin like this (dont know if it works)
And than make a model observer to, that on create if email == all you irritrade trough all users to create the records in your DB
Forms\Components\Select::make('email')
->label(__('Seleccione el alumno (Cliente)'))
->options(function () use ($tenant) {
$riders = Rider::whereHas('companies', function ($query) use ($tenant) {
$query->where('companies.id', $tenant->id);
})->orderBy('name')->get();
// Construir el array de opciones para el Select
$options = $riders->pluck('name', 'email')->toArray();
$options[] = ['All users','all'];
return $options;
})
->required(),
Forms\Components\Select::make('email')
->label(__('Seleccione el alumno (Cliente)'))
->options(function () use ($tenant) {
$riders = Rider::whereHas('companies', function ($query) use ($tenant) {
$query->where('companies.id', $tenant->id);
})->orderBy('name')->get();
// Construir el array de opciones para el Select
$options = $riders->pluck('name', 'email')->toArray();
$options[] = ['All users','all'];
return $options;
})
->required(),
I have created something a little different finally, create only one email with a value, if it is for all users I insert a value 1 in the table, otherwise it will be a value of zero placing the email to which it is directed in another field, finally my code is this:
public static function form(Form $form): Form
{
$tenant = Filament::getTenant();
$selectedEmail = Request::input('email');
$allSelected = ($selectedEmail == 'all') ? 1 : 0;
return $form
->schema([
Forms\Components\TextInput::make('name')
->label('Asunto')
->required(),
Forms\Components\Select::make('email')
->label(__('Seleccione a quien desea enviar la notificación'))
->options(function () use ($tenant) {
// Obtener los riders por el company_id del tenant
$riders = Rider::whereHas('companies', function ($query) use ($tenant) {
$query->where('companies.id', $tenant->id);
})->orderBy('name')->get();
// Construir el array de opciones para el Select
$options = ['all' => 'Enviar este mensaje a todos los clientes']; // Opción "todos"
foreach ($riders as $rider) {
$options[$rider->email] = $rider->name . ', RUT= ' . $rider->rut;
}
return $options;
})
->required(),
Forms\Components\RichEditor::make('reason')
->label('Contenido del mensaje')
->required(),
Hidden::make('email_for_all')
->default($allSelected),
]);
}
public static function form(Form $form): Form
{
$tenant = Filament::getTenant();
$selectedEmail = Request::input('email');
$allSelected = ($selectedEmail == 'all') ? 1 : 0;
return $form
->schema([
Forms\Components\TextInput::make('name')
->label('Asunto')
->required(),
Forms\Components\Select::make('email')
->label(__('Seleccione a quien desea enviar la notificación'))
->options(function () use ($tenant) {
// Obtener los riders por el company_id del tenant
$riders = Rider::whereHas('companies', function ($query) use ($tenant) {
$query->where('companies.id', $tenant->id);
})->orderBy('name')->get();
// Construir el array de opciones para el Select
$options = ['all' => 'Enviar este mensaje a todos los clientes']; // Opción "todos"
foreach ($riders as $rider) {
$options[$rider->email] = $rider->name . ', RUT= ' . $rider->rut;
}
return $options;
})
->required(),
Forms\Components\RichEditor::make('reason')
->label('Contenido del mensaje')
->required(),
Hidden::make('email_for_all')
->default($allSelected),
]);
}