Skip to content

Search on pivot table #72

Open
Open
@anzalpa

Description

@anzalpa

@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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions