doujiao1180
2019-07-14 10:31
浏览 87
已采纳

如何修复未定义的变量:laravel视图中的用户

I'm using this package spatie-laravel-permission

to add roles and permissions to my website.

in my admin dashboard, I fetched all users and set a delete method.


public function user_destroy ($id){

        $user_delete= User::findOrFail($id);

            $user_delete->removeRole($user_delete->getRoleNames());
            $user_delete->delete();


        return view('admin.users.index')->with('success','User is deleted');
    }

so using the code above does not remove the user and his role.

I get this error :

Return value of App\User::getStoredRole() must implement interface Spatie\Permission\Contracts\Role, instance of Illuminate\Support\Collection returned


i tried to use PHP detach() :

 $user_delete->roles()->detach();

It is actually working but I get this error :

Undefined variable: users (View: resources\views\admin\users\index.blade.php)

The PHP $users are defined in the index view like this :


public function mainUsers(){

       $users = User::latest()->paginate(5);
       return view('admin.users.index',compact('users'));

   }

I don't know what is the problem when I use PHP detach() the $users are defined and I get all the users on my index.

Any idea of how I can solve this problem:?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douzhao2047 2019-07-14 10:47
    已采纳

    it turns out that I'm returning a view which expecting a $users instead of redirecting to the index of users.

    changing from :

         return view('admin.users.index')->with('success','User is deleted');
    

    TO :

    
    return redirect()->route('users.index')->with('success','User is deleted');
    
    

    fixed the whole darn problem

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题