protonemedia / laravel-cross-eloquent-search

Laravel package to search through multiple Eloquent models. Supports sorting, pagination, scoped queries, eager load relationships and searching through single or multiple columns.
https://protone.media/blog/search-through-multiple-eloquent-models-with-our-latest-laravel-package
MIT License
1.09k stars 80 forks source link

Search on pivot table #72

Open anzalpa opened 2 years ago

anzalpa commented 2 years ago

@pascalbaljet Is it possible to search on pivot table?

I have the following models (participant belongsToMany project),

participants
    id

participant_project     
    participant_id
    project_id
    first_name
    last_name

projects
    id  

Getting Column not found: 1054 Unknown column 'project_participant.id' in 'field list' error when I try this; as I don't have an id column in the pivot table.

Search::new()
    ->add(\App\Order::whereIn('project_id', $projects), 'confirmation_number')
    ->add(\App\ProjectParticipant::whereIn('project_id', $projects), ['first_name', 'last_name'])
    ->orderByRelevance()
    ->beginWithWildcard()
    ->paginate(20)
    ->search($request->search);

Also, I tried using join, but not working as expected ->add(\App\Participant::join('project_participant', fn($join) => $join->on('participants.id', '=', 'project_participant.participant_id')->where('project_participant.project_id', 8)), ['project_participant.first_name', 'project_participant.last_name'])

Is there any way to achieve this?

pascalbaljet commented 1 year ago

Which model do you want in the search results? Participant, Project, or do you want the ParticipantProject pivot as result?

anzalpa commented 1 year ago

@pascalbaljet I want to search the first_name and last_name on the pivot table(participant_project)

ascepanovic commented 1 year ago

@anzalpa did u mange to resolve this ?