Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MahmoudAdelAli's avatar

Get id of specific value in many to many relation

hello , i have here 2 tables ,

clients 
ClientNotes

and i need to get id for the note and this is very easy

  Note N-{{$client_note->id}}

but i need this id start from 1 inside every client page cause if he insert note in client one the note started with 1 and 2 etc ... and when i insert note in another client it's started from 3 cause there's 2 notes assigned to the previous client

0 likes
4 replies
tykus's avatar
tykus
Best Answer
Level 104

If you are iterating over the Collection of Notes, you can use the Loop Variable (iteration property) to get an index.

@foreach ($client->clientNotes as $clientNote)
    Note N-{{ $loop->iteration }}
	// ... etc

Otherwise, you will need to share more code/database schema to help us understand your exact use case.

MahmoudAdelAli's avatar

Solved with If you are using blade, the laravel's automatically generated $loop variable might help. Like this:

  Note N- {{$loop->iteration}}

Source

https://stackoverflow.com/questions/65534039/how-to-automatically-number-each-row-in-a-table-in-laravel
Snapey's avatar

depends if the note number is just cosmetic or if the user will use the notes number in some way

Also, what happens if notes are deleted, do notes keep the original number?

Please or to participate in this conversation.