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

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度