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

ederson's avatar

ManyToMany and string Ids

Has anyone faced problems with Many to many relations and non Int ids?

I tried the example in the documentation page using char instead of int for ids to verify this in case my code had errors

here is the query using INT ids for user with id 3

SELECT `roles`.*,
  `user_roles`.`user_id` AS `pivot_user_id`,
  `user_roles`.`role_id` AS `pivot_role_id`
FROM `test_categories`
  INNER JOIN `user_roles` ON `roles`.`id` = `user_roles`.`role_id`
WHERE `user_roles`.`user_id` in (3)

if i have string iDs for example user_3

SELECT `roles`.*,
  `user_roles`.`user_id` AS `pivot_user_id`,
  `user_roles`.`role_id` AS `pivot_role_id`
FROM `test_categories`
  INNER JOIN `user_roles` ON `roles`.`id` = `user_roles`.`role_id`
WHERE `user_roles`.`user_id` in (0)

the last where clause does not use the appropriate id, it is 0 instead of user_3

0 likes
8 replies
Sinnbeck's avatar

It's usually a bad idea to use string ids. Be aware it will slow down your database as your indexing for strings is way slower.

ederson's avatar

@Sinnbeck yes i know. It is a pretty old app i have to clean up and this is one of the todos although the tables will not have more than a few hundred records.

regardless though it looks strange that belongsToMany does not like string IDs

Snapey's avatar

@ederson you need to tell your models to not assume autoincrementing id

protected $incrementing=false;

I think thats the variable, you may need to google it, it's been a while

ederson's avatar

@Snapey no that is not it I had already done this. Every other function that uses the id works as expected for example a one to one relationship , the find functions. The problem appears only with many to many.

ederson's avatar

I use for troubleshooting the models in the example in the decumentation page. They only have the relation

namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
 
class Role extends Model
{   
    public function users()
    {
        return $this->belongsToMany(User::class,'user_roles');
    }
}
namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
 
class User extends Model
{   
	public $incrementing = false;

    public function roles()
    {
        return $this->belongsToMany(Role::class,'user_roles');
    }
}
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@ederson set key type as string as well

protected $keyType = 'string';
ederson's avatar

!!!!!!!! I had no idea about this variable! Now it works

I find the behavior a bit strange tbh. Even with belongstomany it did not through an error i accidentally saw that it fetches too many models and started looking for the source of the problem. Considering that other functions worked i thought it had something to do with the relationship and not the model

I had moved to int ids cause that is the best option but still i was curious about this

Thanks @Sinnbeck and @Snapey

Please or to participate in this conversation.