Table Data Visibility
I created a dispatch system in Laravel Filament where users can assign Workorders to Vendors. I get all my Vendors from my User model which has a 'role' of 'Vendor'. I've also set up a relationship between my User and Workorder model.
Currently, all Workorders can be seen by the Vendors even if that WO is not assigned to them. I want to add a function where 'Vendors' can only see the Workorder that was assigned to them.
Here's what I did which is throwing me an error of: Cannot use "::class" on value of type null
public static function table(Table $table): Table
{
return $table
->query(function (Builder $query) {
// Base query that fetches all work orders
$baseQuery = $query->select('*')->from('workorders');
// If user is a Vendor, modify the base query to only include their work orders if (Auth::user()->hasRole('Vendor')) { return $baseQuery->where('user_id', Auth::id()); }
// Otherwise, return the base query return $baseQuery; })
// If user is a Vendor, modify the base query to only include their work orders if (Auth::user()->hasRole('Vendor')) { return $baseQuery->where('user_id', Auth::id()); }
// Otherwise, return the base query return $baseQuery; })
1 Reply
Here's my schema for User and Workorder for reference:
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->boolean('user_preferred')->nullable();
$table->rememberToken();
$table->timestamps();
});
Schema::create('workorders', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('customer_id')->nullable();
$table->unsignedBigInteger('user_id')->nullable();
$table->string('wo_number')->nullable();
$table->string('wo_problem')->nullable();
$table->string('wo_problem_type')->nullable();
$table->string('wo_description')->nullable();
$table->string('wo_customer_po')->nullable();
$table->string('wo_asset')->nullable();
$table->string('wo_priority')->nullable();
$table->string('wo_trade')->nullable();
$table->string('wo_category')->nullable();
$table->string('wo_tech_nte')->nullable();
$table->string('wo_schedule')->nullable();
$table->string('wo_status')->nullable();
$table->timestamps();
});
Make it worked! π
->modifyQueryUsing(function (Builder $query) {
$user = Auth::user();
// If the user is a Vendor, modify the query to only show their assigned workorders.
if($user->hasRole('Vendor')) {
$query->where('user_id', $user->id);
}
return $query;
})