Show data in Select, based on relation manager

I need your help with a query. In this case, the Task model, will generate all Tasks. But I only want the tasks that are in the relation manager of the project that I select via Select
public static function form(Form $form): Form
{
return $form
->schema(Card::make([
Select::make('activity_id')
->label('Activity')
->options(Activity::all()->pluck('name', 'id'))
->searchable()
->required(),
Forms\Components\Textarea::make('description')
->required()
->maxLength(500),
Forms\Components\DateTimePicker::make('date')
->withoutTime()
->required()
->columnSpan(1),
Forms\Components\TimePicker::make('time')
->withoutDate()
->withoutSeconds()
->required()
->columnSpan(1),
Select::make('project_id')
->label('Project')
->options(Project::all()->pluck('name', 'id'))
->searchable()
->required()
->columnSpan(1),
Select::make('worker_user_id')
->label('Worker')
->options(User::all()->pluck('name', 'id'))
->searchable()
->required()
->disabled(!auth()->user()->can('show_everything_time::registration'))
->default(Auth()->id())
->columnSpan(1),
Select::make('task_id')
->label('Select Task')
->options(Task::all()->pluck('name', 'id'))
->columnSpan(1),
])->columns(2));
}
public static function form(Form $form): Form
{
return $form
->schema(Card::make([
Select::make('activity_id')
->label('Activity')
->options(Activity::all()->pluck('name', 'id'))
->searchable()
->required(),
Forms\Components\Textarea::make('description')
->required()
->maxLength(500),
Forms\Components\DateTimePicker::make('date')
->withoutTime()
->required()
->columnSpan(1),
Forms\Components\TimePicker::make('time')
->withoutDate()
->withoutSeconds()
->required()
->columnSpan(1),
Select::make('project_id')
->label('Project')
->options(Project::all()->pluck('name', 'id'))
->searchable()
->required()
->columnSpan(1),
Select::make('worker_user_id')
->label('Worker')
->options(User::all()->pluck('name', 'id'))
->searchable()
->required()
->disabled(!auth()->user()->can('show_everything_time::registration'))
->default(Auth()->id())
->columnSpan(1),
Select::make('task_id')
->label('Select Task')
->options(Task::all()->pluck('name', 'id'))
->columnSpan(1),
])->columns(2));
}
20 Replies
Matthew
MatthewOP2y ago
I want to create a entry in the TimeRegistration resource. And I have 2 Selects. The Project resource has a task relation manager, where you can assign tasks. When I select a project in the Select, I only want to be able to select the tasks (in the task Select) associated with that Project
Dan Harrin
Dan Harrin2y ago
maybe use a SelectFilter?
Matthew
MatthewOP2y ago
isnt the selectfilter only at tables? This is a form
Dan Harrin
Dan Harrin2y ago
the relation manager is a table
Matthew
MatthewOP2y ago
Correct, but in this case im in the TimeRegistration resource, and when Im creating a entry, then I should be able to choose based on what task is available for that project
Dan Harrin
Dan Harrin2y ago
ohh do you want a dependant select search "dependant select" in the docs
Matthew
MatthewOP2y ago
I just saw your video
Matthew
MatthewOP2y ago
Dan Harrin
YouTube
Dependant Select Inputs - Filament TALL Stack Form Builder
This video, we are using the filament/forms package. You can read the full documentation at: https://filamentadmin.com/docs/forms You can find out more about Filament on our website: https://filamentadmin.com Follow me on Twitter for more tutorials and other TALL stack tips: https://twitter.com/danjharrin If you found this video useful, pleas...
Dan Harrin
Dan Harrin2y ago
yeah
Matthew
MatthewOP2y ago
Whats supposed to be on the left side of pluck?
Matthew
MatthewOP2y ago
Dan Harrin
Dan Harrin2y ago
$project->tasks()
Matthew
MatthewOP2y ago
No, I meant instead of 'task; or 'title' like you have in your video, what is supposed to be there?
Matthew
MatthewOP2y ago
I get this error
Matthew
MatthewOP2y ago
Maybe smth missing in the model?
class TimeRegistration extends Model
{
use HasFactory;

protected $fillable = ['description', 'activity_id', 'date', 'time', 'project_id', 'worker_user_id'];

protected $visible = ['id', 'description', 'activity_id', 'date', 'time', 'project_id', 'worker_user_id'];

protected $casts = [
'date' => 'date'
];

public function project() {
return $this->belongsTo(Project::class,'project_id');
}

public function activity() {
return $this->belongsTo(Activity::class);
}

public function worker() {
return $this->belongsTo(User::class, 'worker_user_id');
}

public function task(){
return $this->belongsTo(Task::class, 'task_id');
}
}
class TimeRegistration extends Model
{
use HasFactory;

protected $fillable = ['description', 'activity_id', 'date', 'time', 'project_id', 'worker_user_id'];

protected $visible = ['id', 'description', 'activity_id', 'date', 'time', 'project_id', 'worker_user_id'];

protected $casts = [
'date' => 'date'
];

public function project() {
return $this->belongsTo(Project::class,'project_id');
}

public function activity() {
return $this->belongsTo(Activity::class);
}

public function worker() {
return $this->belongsTo(User::class, 'worker_user_id');
}

public function task(){
return $this->belongsTo(Task::class, 'task_id');
}
}
Dan Harrin
Dan Harrin2y ago
it's the name of the column that provides labels for the select is there a task name or title ?
Matthew
MatthewOP2y ago
''name"
class Task extends Model
{
use HasFactory;

protected $fillable = ['name', 'description', 'deadline', 'project_id', 'responsible_user_id', 'is_completed'];

public function project() {
return $this->belongsTo(Project::class);
}

public function owner() {
return $this->belongsTo(User::class, 'responsible_user_id');
}

public function taskstatusName()
{
return $this->belongsTo(TaskStatus::class, 'is_completed');
}
}
class Task extends Model
{
use HasFactory;

protected $fillable = ['name', 'description', 'deadline', 'project_id', 'responsible_user_id', 'is_completed'];

public function project() {
return $this->belongsTo(Project::class);
}

public function owner() {
return $this->belongsTo(User::class, 'responsible_user_id');
}

public function taskstatusName()
{
return $this->belongsTo(TaskStatus::class, 'is_completed');
}
}
Dan Harrin
Dan Harrin2y ago
so pluck('name', 'id')
Matthew
MatthewOP2y ago
Now the task, shows projects, not the tasks associated with that proj
Matthew
MatthewOP2y ago
I got it!
Want results from more Discord servers?
Add your server