Preload()

Hello! I have this structure: * Users * Business -> Users (related with table USERS) -> Branches (where i related users where has relation with the business) When i search in the select, run ok. but the preload get all users, i want get the users where has relation with the business) Whats the error? thanks
Select::make('users')
->relationship('users', 'name')
->multiple()
->searchable()

->getSearchResultsUsing(function (string $search): array {
return User::whereHas('businesses', function (Builder $query) {
$query->whereKey($this->ownerRecord->id);


})
->where(function (Builder $query) use ($search) {
$query->where('name', 'like', "%{$search}%");
})
->limit(50)
->pluck('name', 'id')
->toArray();
})
->getOptionLabelsUsing(function (array $values): array {

return User::whereIn('id', $values)->pluck('name', 'id')->toArray();
})
->preload()

Select::make('users')
->relationship('users', 'name')
->multiple()
->searchable()

->getSearchResultsUsing(function (string $search): array {
return User::whereHas('businesses', function (Builder $query) {
$query->whereKey($this->ownerRecord->id);


})
->where(function (Builder $query) use ($search) {
$query->where('name', 'like', "%{$search}%");
})
->limit(50)
->pluck('name', 'id')
->toArray();
})
->getOptionLabelsUsing(function (array $values): array {

return User::whereIn('id', $values)->pluck('name', 'id')->toArray();
})
->preload()

3 Replies
cheesegrits
cheesegrits15mo ago
Use a closure as the third parameter on the relationship() method to constrain the relation query, similar to how you do it in the search.
lucianosnchz
lucianosnchzOP15mo ago
ITS RUNNING, THANK'S
cheesegrits
cheesegrits15mo ago
Remember to mark the solution, so it gets indexed by Google and helps the next person searching.
Want results from more Discord servers?
Add your server