Skip to content

How to access a many-to-many relationship? #1386

Answered by lrljoe
kevinrueda asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @kevinrueda

Three elements here:

  1. Avoid using "select" in your builder() method, as it will conflict with the internal mechanism that selects the columns for you.

Once that's resolved:

  1. Keep in mind that you will need to define them as a "label" Column, e.g, this implodes the "name" of the branch, with commas:
Column::make('Branches')
    ->label(fn ($row) => $row->branches->pluck('name')->implode(', ')),

  1. Using with will forcibly load the relation for you, avoid defining a variable where you don't need to, so long as you're certain it is defined, and restrict the "with" to only return the fields you need (e.g. id, name).
public function builder(): Builder
{
  return Product::with…

Replies: 1 comment 11 replies

Comment options

lrljoe
Oct 5, 2023
Collaborator Sponsor

You must be logged in to vote
11 replies
@lrljoe
Comment options

lrljoe Oct 5, 2023
Collaborator Sponsor

@kevinrueda
Comment options

@kevinrueda
Comment options

@lrljoe
Comment options

lrljoe Oct 5, 2023
Collaborator Sponsor

@kevinrueda
Comment options

Answer selected by kevinrueda
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants