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

armanyum's avatar

Facing issues during Laravel 5 to Laravel 10 Migration

I have just migrated to Laravel 10 from Laravel 5, the project used entrust for role and permissions, I have successfully migrated but now are facing issues. first during login I faced an error App\Role not found when I tried to do this $user->roles So I added this in User Model and it worked fine public function roles() { return $this->belongsTo(Role::class, 'role_id'); } Now when I try to delete a user like this $user->delete(); it gives error Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsTo::sync() Any suggestions on how I can solve this one?

0 likes
7 replies
JussiMannisto's avatar

Your method is named roles(), implying users can have multiple roles (many-to-many relation). But the method returns a belongsTo, meaning each user has exactly one role (one-to-many relation). Which is it?

Show the user and role models. But when you do, wrap the code in three backticks like this:

```

Some code

```

That will format your code.

armanyum's avatar

@JussiMannisto User Model

<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Zizaco\Entrust\Traits\EntrustUserTrait;

class User extends Authenticatable
{
    use Notifiable;
    use HasFactory;
    use EntrustUserTrait;

    protected $connection = 'mysql';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password','modules_allow'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
    public function permission(){

    }

    public function roles()
    {
        return $this->belongsTo(Role::class, 'role_id');
    }
}

Role Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Zizaco\Entrust\EntrustRole;


class Role extends EntrustRole
{
    use HasFactory;
    protected $connection = 'mysql';

}

armanyum's avatar

Note: I added this in users model because I was facing an error App\Role not found where a function was trying to access role

    public function roles()
    {
        return $this->belongsTo(Role::class, 'role_id');
    }

this is what that piece of code was

        $userRole = $user->roles->pluck('id','id')->toArray();
JussiMannisto's avatar

The method is named roles() but it returns a belongsTo. Do your users have multiple roles or not?

The role class doesn't show anything because it only extends the EntrustRole class.

I googled the zizaco/entrust package that you seem to be using. The package is for Laravel 5 and hasn't been updated in 8 years. A cursory glance at the composer.json requirements shows that it's not compatible with Laravel 10. So... what's going on here?

armanyum's avatar

@JussiMannisto yes its not compatible and I am going to update it to spatie but for that I need to first check all the functionality. the zizaco\entrust does allow users to have multiple roles

JussiMannisto's avatar

@armanyum So you're using a package that's not compatible with the framework and hasn't been updated in 8 years. Of course it's not going to work as is.

I don't know how you even managed to install the package, given the conflicting requirements. Did you fork it and manually change the requirements?

You can of course try to rewrite the package to work on Laravel 10. I would guess it takes more time than just moving to a different package. A lot has changed in the last 8 years.

armanyum's avatar

@JussiMannisto yeah lets just say setting it up was a hectic task, but I managed to install and run it but since its so old its giving some expected issues, its an old product thats been running as is, I need to now update it

Please or to participate in this conversation.