Antonella's avatar

sort the calculated field date

the Last Activity field is the last date among all customers. which is calculated as follows:

  DateTime::make('Last Activity','last_progress_date', function () {
                $card = TrelloCard::where('customer_id',$this->id)->orderBy('last_progress_date', 'DESC')->first();
                return  $card->last_progress_date;
            })->format('YYYY-MM-DD')->sortable(),

unfortunately when I go to do the sort I get the following error:

unknown column 'last_progress_date' in order clause

How can I from this date calculated as follows:

                    $card = TrelloCard::where('customer_id',$this->id)->orderBy('last_progress_date', 'DESC')->first();

order it later for the date I got

0 likes
2 replies
piljac1's avatar

Assuming your orderBy doesn't throw any error on your TrelloCard and the page loads without errors initially, it means the orderBy throws an error when sorting (which I think is what I understood by reading your post). If that's the case, it is caused by the fact that sorting will apply an orderBy clause to your current model (which represents a customer) and since your customer doesn't have a last_progress_date column, it crashes. To solve this, you could keep the same logic, but change your DateTime to Text and remove your second parameter (last_progress_date). You should also put the format inside your callback.

Note: this will not display this date in your forms since it acts as a computed field. But according to your logic, it shouldn't be part of your forms.

piljac1's avatar

Update: I just remembered computed fields are not sortable. What you could probably do though (untested) is to customize your base query to add a joinSub containing the subquery to fetch the customer's last_progress_date. That way you could probably sort the column.

Look up Index Query

1 like

Please or to participate in this conversation.