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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题