laravel Filametphp get table data by auth user id

986 views Asked by At

I am working with filament php and I am stuck here as I want table data to be user specific and want to get loggedin user data only

    public static function table(Table $table): Table
    {

        $user = Auth::user(); // Get the authenticated user
        $userId = $user->id; // Assuming 'id' is the user ID field in your user model
        return $table //I want to access this data by user ID 
            ->columns([
                Tables\Columns\TextColumn::make('user_id')
                    ->searchable(),
                Tables\Columns\TextColumn::make('title')
                    ->searchable(),
                Tables\Columns\TextColumn::make('slug')
                    ->searchable(),
                Tables\Columns\TextColumn::make('status')
                    ->searchable(),
            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ])
            ->emptyStateActions([
                Tables\Actions\CreateAction::make(),
            ]);
    }
1

There are 1 answers

0
AmooAti On

You can simply use modifyQueryUsing method on a Table instance.

Read here for more.

For your use case it will be:

    public static function table(Table $table): Table
    {

        $user = Auth::user(); // Get the authenticated user
        $userId = $user->id; // Assuming 'id' is the user ID field in your user model
        return $table //I want to access this data by user ID
            ->modifyQueryUsing(function (Builder $query) use ($userId) {
                $query->where('user_id', $userId);
            })
            ->columns([
                Tables\Columns\TextColumn::make('user_id')
                    ->searchable(),
                Tables\Columns\TextColumn::make('title')
                    ->searchable(),
                Tables\Columns\TextColumn::make('slug')
                    ->searchable(),
                Tables\Columns\TextColumn::make('status')
                    ->searchable(),
            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ])
            ->emptyStateActions([
                Tables\Actions\CreateAction::make(),
            ]);
    }

But I suggest If you just use the user_id for this filtering, move it to the scope of the query modifier function instead of initiating it out of the scope and then using it in the function. Something like this:

public static function table(Table $table): Table
    {
        return $table //I want to access this data by user ID
            ->modifyQueryUsing(function (Builder $query) {
                $userId = Auth::user()->id;
                $query->where('user_id', $userId);
            })
            // ...

    }