Oxygarum
Oxygarum
FFilament
Created by Oxygarum on 12/1/2024 in #❓┊help
Access JSON and display in a table
Hey, probably I am overlooking something really obvious. I have a table and in that want to access a JSON that is stored in a database column in another model. When trying to debug with log::info it is displaying me the info correctly, but I am not sure how to let ->query use the data in the JSON My code for accessing the JSON looks like this:
public static function table(Table $table): Table
{
$hrRecord = Hrdatabase::where('badgeofemployee', $badgeNumber)->first();


$visitedShifts = json_decode($hrRecord->visitedshifts, true);

Log::info('Visited Shifts: ', $visitedShifts);



return $table
->query($visitedShifts)

->columns([
Tables\Columns\TextColumn::make('shift_date')
->label('Date of Shift')
->getStateUsing(fn ($record) => $record->shift_date ?? 'N/A'),

Tables\Columns\TextColumn::make('shift_type')
->label('Shift Type')
->getStateUsing(fn ($record) => $record->shift_type ?? 'N/A'),

Tables\Columns\TextColumn::make('shift_description')
->label('Shift Description')
->getStateUsing(fn ($record) => $record->shift_description ?? 'N/A'),

Tables\Columns\TextColumn::make('shift_duration')
->label('Shift Duration')
->getStateUsing(fn ($record) => $record->shift_duration ?? 'N/A'),

Tables\Columns\TextColumn::make('shift_supervisor')
->label('Shift Supervisor')
->getStateUsing(fn ($record) => implode(', ', $record->shift_supervisor ?? [])),
])
public static function table(Table $table): Table
{
$hrRecord = Hrdatabase::where('badgeofemployee', $badgeNumber)->first();


$visitedShifts = json_decode($hrRecord->visitedshifts, true);

Log::info('Visited Shifts: ', $visitedShifts);



return $table
->query($visitedShifts)

->columns([
Tables\Columns\TextColumn::make('shift_date')
->label('Date of Shift')
->getStateUsing(fn ($record) => $record->shift_date ?? 'N/A'),

Tables\Columns\TextColumn::make('shift_type')
->label('Shift Type')
->getStateUsing(fn ($record) => $record->shift_type ?? 'N/A'),

Tables\Columns\TextColumn::make('shift_description')
->label('Shift Description')
->getStateUsing(fn ($record) => $record->shift_description ?? 'N/A'),

Tables\Columns\TextColumn::make('shift_duration')
->label('Shift Duration')
->getStateUsing(fn ($record) => $record->shift_duration ?? 'N/A'),

Tables\Columns\TextColumn::make('shift_supervisor')
->label('Shift Supervisor')
->getStateUsing(fn ($record) => implode(', ', $record->shift_supervisor ?? [])),
])
For the log::info I receive back my test entry of "[{"shift_id":1,"shift_type":"TestType","shift_description":"TestDescription","shift_duration":25,"shift_date":"2024-11-27","shift_supervisor":["John Doe"]}]"
3 replies
FFilament
Created by Oxygarum on 8/9/2024 in #❓┊help
Limit display of entries in table
Hey, I know there is probably something overly easy I am overlooking, but I can not figure it out... I have made a widget displaying a table, but now want to only show the 5 most recent records. How can this be done? I first thought about limit(), but its not working. Thanks 🙂
3 replies
FFilament
Created by Oxygarum on 2/8/2024 in #❓┊help
Problem with creating query to show differences in values from variables and sort them
Hey Hey, I have a database that is getting input through a DeliveryLog resource form and so forth. In that employees need to input their deliveries handled within a shift. For simplicity I just called them deliveriesatstartofshift and deliveriesatendofshift. That is all working perfectly. Now I created a new Resource, Model and so forth in which I want to use a query to display the employees that made the most deliveries within the last seven days. For this I would want to take the difference between deliveriesatstartofshift and deliveriesatendofshift for all shifts the employees did in that time period. Employees can make multiple shifts of course within the last days so those differences would need to get added up. I worked for a while on it already, but am always running into the error "Cannot use "::class" on value of type null". This is what I made so far for the query:
public static function table(Table $table): Table
{


return $table


->query(function (Builder $query) {
return $query
->from('delivery_logs', 'dl')
->select(
'dl.badge',
DB::raw('SUM(dl.deliveriesatendofshift - dl.deliveriesatstartofshift) AS total_deliveries')
)
->whereBetween('dl.created_at', [Carbon::now()->subDays(7), Carbon::now()])
->groupBy('dl.badge', 'dl.id')
->orderByDesc('total_deliveries');
})
->defaultSort('total_deliveries', 'desc')
public static function table(Table $table): Table
{


return $table


->query(function (Builder $query) {
return $query
->from('delivery_logs', 'dl')
->select(
'dl.badge',
DB::raw('SUM(dl.deliveriesatendofshift - dl.deliveriesatstartofshift) AS total_deliveries')
)
->whereBetween('dl.created_at', [Carbon::now()->subDays(7), Carbon::now()])
->groupBy('dl.badge', 'dl.id')
->orderByDesc('total_deliveries');
})
->defaultSort('total_deliveries', 'desc')
Hopefully I explained it well enough 😄
5 replies