简体   繁体   English

显示数据为user_id

[英]Displays data as user_id

I am logged in with the user I have and all I want is my account can only see the data I just sent. 我已经使用我的用户身份登录,我想要的只是我的帐户只能看到我刚刚发送的数据。 Here I find a problem that is able to see the data of others. 在这里,我发现了一个能够查看其他人数据的问题。

First this is Model Data 首先这是模型数据

public function user()
{
// Each data is owned by only one user
return $this->belongsTo('App\User');
}

And then Model User 然后是模型用户

public function data()
{
// Each user will have a lot of data
return $this->hasMany('App\Data');
}

The last this is controller 最后这是控制器

public function index()
{
  $show_data = Auth::user()->data();
  $show_data = Data::where('user_id', '=', $user->id)
                 ->paginate(10);
  $amount_of_data = Data::count();
  return view('data.index', compact('show_data', 'amount_of_data'));
}

Edit index method to this and try: 为此编辑索引方法,然后尝试:

public function index()
{
    $show_data = auth()->user()->data()->paginate(10);
    $show_data_count = count(auth()->user()->data);

    return view('data.index', compact('show_data', 'show_data_count'));
}

App Model For Data 数据应用模型

public function data()
{
  // Each user will have a lot of data
  return $this->hasMany('App\Data','user_id');
} 

Final Controller 最终控制人

public function index()
{
    $show_data = Auth::user()->data()->paginate(10);
    $amount_of_data = count($show_data['data']);
    return view('data.index', compact('show_data', 'amount_of_data'));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM