dimanche 8 octobre 2017

Laravel elequent many to many and polymorphic relarion

I have a user , role, role_user and a photo(Polymorphic ) model class and related table in db. I i am using many to many relationship for role and user in user model i use a trait( using "use HasRoles"):

HasRoles

    trait HasRoles
{

    /**
     * A user may have multiple roles.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function roles()
    {
        return $this->belongsToMany(Role::class);
    }

    /**
     * Assign the given role to the user.
     *
     * @param  string $role
     * @return mixed
     */
    public function assignRole($role)
    {
        return $this->roles()->save(
            Role::whereName($role)->firstOrFail()
        );
    }

    /**
     * Determine if the user has the given role.
     *
     * @param  mixed $role
     * @return boolean
     */
    public function hasRole($role)
    {

        if (is_string($role)) {

            return $this->roles->contains('name', $role);
        }

        return !! $role->intersect($this->roles)->count();
    }
}

User controller :

 public function __construct(User $user)
    {

        $this->middleware('auth');

        $this->user = $user;

    }

    /**
     * $userList and $dataArr from  Erp\Lists\UserList containing the array and the model to pass
     * for creating dynamic lists
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(UserList $list)
    {
        $usersList =$list;
        $model = $this->user;
        $users = $this->user->paginate(5);

        $usersWithPhotos = array();
        foreach($users as $user){

            if( !$user->hasRole('Teacher')
                && !$user->hasRole('Student')
                && !$user->hasRole('Guardian')
                && count($user->photos)>0)

                $usersWithPhotos[$user->photos->last()->name] = $user;
        }

and view:

@foreach($usersWithPhotos as $photo => $user)



                                                <tr>
                                                <td></td>

                                                <td class="text-center">{!!  Html::image('imagecache/dummy/'.$photo) !!}</td>
                                                
                                                <td class="text-center">
                                                    @if(!is_null($user->translate($defaultLocale)))

                                                        
                                                    @else

                                                        No Name Given in ...@if($locale=='en') English @else  Bangla @endif
                                                    @endif

                                <div class="pagination">{!!   str_replace('/?','?',$users->render() ) !!} </div>

What i want:

i want to get all users except role "student and teacher, and guardian" and also want use pagination with the result. now i am using two eloquent . one for pagination and another for get correct user list. also check if the user has photo in photos table. The problem is that i got the user list but pagination got long(1,2,3,4,----) for one user list. how can i get correct list with pagination?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire