Agus Suparman Agus Suparman -4 years ago 77
PHP Question

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

Answer Source

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'));
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download