简体   繁体   中英

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'));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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