Replies: 3 comments 11 replies
-
List of all features does not depend on the organisations so why the need to link it? You can use the append. in your model
https://laravel.com/docs/10.x/eloquent-collections#method-append |
Beta Was this translation helpful? Give feedback.
-
@kohenkatz sorry for the years late reply, but did you ever figure out a solution to this? I have a parent with children that are sometimes related through a pivot table, but I would like to load everything as if they are always related. Seems like a similar type of problem. Event
Events have volunteers through teams, but they also have all teams, but sometimes the teams will be empty from event to event. Teams and Volunteers are a fixed pool that largely stays static between Events, but every event has different volunteers on each team based on a pivot I would love to be able to define all teams as belonging to an Event, if if there is nobody currently scheduled. |
Beta Was this translation helpful? Give feedback.
-
@daronspence Request: list of all features, indicating which of those features are enabled for the given organization SELECT
f.id,
f.friendly_name,
of.id IS NOT NULL AS enabled
FROM features f
LEFT OUTER JOIN organization_features of
ON f.id = of.feature_id AND of.organization_id=?
ORDER BY 1; This implies enabled if the pivot exists for it. class Feature extends BaseModel
{
protected $fillable = ['friendly_name'];
public function organizationFeaturePivots(): HasMany
{
return $this->hasMany(OrganizationFeaturePivot::class, 'feature_id', 'id')
}
}
class OrganizationFeaturePivot extends BaseModel
{
protected $fillable = ['organization_id', 'feature_id'];
}
class Organization extends BaseModel
{
}
class FeaturesService extends BaseResourceService
{
/**
* @inheritDoc
*/
protected function setBaseModel(): void
{
$this->model = new Feature();
}
/**
* @inheritDoc
*/
public function list(array $request): Builder
{
// Request: list of all features, indicating which of those features are enabled for the given organization
$builder = $this->model::query();
$builder->withExists(['organizationFeaturePivots as enabled' => function (Builder $builder) use ($request): void {
$builder->where('organization_id', $request['organization_id'] ?? null);
}])
return $builder;
}
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have models
Organization
andFeature
, with abelongsToMany
relationship between them (i.e. an organization has multiple features, and a feature can be used by multiple organizations).I am being asked for a simple "list of all features, indicating which of those features are enabled for the given organization."
One of my coworkers used the following query to do it:
I'm trying to figure out if there is a more "Laravel-style" way to write this.
Obviously I can just translate this query to use the query builder, but is there a better way?
Beta Was this translation helpful? Give feedback.
All reactions