php - 如何在 Laravel 中基于用户显示数据列表

标签 php mysql laravel eloquent

我有两个表(实际上是三个表,但在这种情况下它只与这两个表相关),Pekerjaan 和 User。两表皆 Eloquent 。用户有许多pekerjaans,并且Pekerjaan属于User。在用户表中,它的状态为“super”和“ppk”。 “Super”是 super 管理员,它可以查看所有数据,而对于“ppk”,它只能查看基于 Pekerjaan 表中他/她的 USER_ID 的某些数据。这是我的 User.php 模型代码:

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

这是 Pekerjaan.php 模型:

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

}

这是我在 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'));
        }  
    }

这是我在 web.php 中的代码:

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

现在,当我以“ppk”身份登录时,它会显示空白表,而我需要的是它将根据用户 ID 显示 pekerjaan 列表。如何实现这一目标?这是我在数据库中的表 pekerjaans: work's table

最佳答案

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

尝试上面的代码,我猜你的路由模型绑定(bind)是正确的。

关于php - 如何在 Laravel 中基于用户显示数据列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72878044/

相关文章:

laravel - Deployer v4.2.1 fatal error "undefined function"带有开箱即用的官方认可的第 3 方配方

php - laravel从另一个表获取用户信息问题

php - 如何将 ->isDirty() 与 laravel ->update() 合并

mysql - VB.NET 连接到 MySQL 服务器数据库

Laravel 4.1 Eloquent - 使用 whereHas 的自定义连接

mysql - MYSQL 的最佳选择结构?

php - 表单操作不允许删除记录

javascript - 对表中的列值求和

php - 如何合并JSON格式的MySQL查询结果

Mysql跨三个表查询?