Home > front end >  How to show list of data based on the user in Laravel
How to show list of data based on the user in Laravel

Time:07-06

I have two table (three actually, but in this context it's only related to these two tables), Pekerjaan and User. Both table are in eloquent. User hasMany pekerjaans, and Pekerjaan belongsTo User. In the User table it has status 'super' and 'ppk'. 'Super' is a super admin whereby it can view all data, and for 'ppk' it can only view certain data based on his/her USER_ID in Pekerjaan's table. Here is my code for User.php model:

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Jetstream\HasProfilePhoto;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Database\Eloquent\Model as Eloquent;

class User extends Authenticatable
{
    use HasApiTokens;
    use HasFactory;
    use HasProfilePhoto;
    use Notifiable;
    use TwoFactorAuthenticatable;

    /**
     * The attributes that are mass assignable.
     *
     * @var string[]
     */
    protected $fillable = [
        'name',
        'email',
        'username',
        'satker',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
        'two_factor_recovery_codes',
        'two_factor_secret',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = [
        'profile_photo_url',
    ];

    public function pekerjaans(){
        return $this->hasMany(Pekerjaan::class);
    }
}

And here is the Pekerjaan.php model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model as Eloquent;

class Pekerjaan extends Eloquent
{
    use HasFactory;

    protected $guarded = [];

    public function penyedia(){
        return $this->belongsTo(Penyedia::class, 'penyedia_id');
   }

   public function user(){
    return $this->belongsTo(User::class, 'user_id');
}

}

Here is what I've tried in AdminController:

 public function tabelpekerjaan(User $user){
        if(Auth::user()->status=='super'){
            $pekerjaan = Pekerjaan::with('penyedia')->paginate();
            return view('admin.datapekerjaan', compact('pekerjaan'));
        }else{
            $pekerjaan = $user->pekerjaans;
            return view('admin.datapekerjaan', compact('pekerjaan'));
        }  
    }

Here is my code in web.php:

Route::get('/datapekerjaan',[AdminController::class,'tabelpekerjaan'])->name('datapekerjaan');

For now it shows me blank table when I logged in as 'ppk', and what I need is it will shows list of pekerjaan based on the user id. How to achieve this? Here is my table pekerjaans in database: work's table

CodePudding user response:

 public function tabelpekerjaan(){
    if(Auth::user()->status=='super'){
        $pekerjaan = Pekerjaan::with('penyedia')->paginate();
        return view('admin.datapekerjaan', compact('pekerjaan'));
    }else{
        $pekerjaan = Auth::user()->pekerjaans;
        return view('admin.datapekerjaan', compact('pekerjaan'));
    }  
}

Try the above code, i guess your route model binding is in correct.

  • Related