Column 'id' in field list is ambiguous select2 edit mode
Hi, i have a problem to take data from relationship belongstomany, i try take data when i edit post and i have this error this '1052 Column 'id' in field list is ambiguous'.
error:SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in field list is ambiguous
SELECT `id` FROM `tags` INNER JOIN `post_tag` ON `tags`.`id` = `post_tag`.`` WHERE `post_tag`.`` = 6
The ambiguity is solved by speciying the table name from where you want to retrieve the id field. Probably your pivot table (post_tag) also has a column named id, so SQL does not know which id column to retrieve.
->pluck('tags.id')
But please consider, and read carefully @snapey answer. It is a much better approach.
In your current code you are executing a different query to the database for each $tagSelect item. If this array has 100 items you are going to execute 100 queries, which will be very slow.
Using @snapey suggestion you would execute just one query instead.
On Laravel docs there is a nice explanation of the N+1 query problem referred in @snapey answer: