reordering based on condition

I have position column in table which I am reordering Is it possible to reorder the position column with a condition that prevents reordering if the position value is 1?
28 Replies
Kenneth Sese
Kenneth Sese2y ago
Hi. Do you mean you mean when you toggle on the reordering button (which I don't see in your screenshot) you want to be able to reorder all the rows EXCEPT the row with position 1?
ba_mbi_07
ba_mbi_07OP2y ago
sorry i shared wrong screenshot
ba_mbi_07
ba_mbi_07OP2y ago
ba_mbi_07
ba_mbi_07OP2y ago
yes i want to reorder all rows except position 1
Kenneth Sese
Kenneth Sese2y ago
One way would be to override the reorderTable method in your ListHomePageHandlers class. I don't think you can disable the row so it won't move, but by overriding the reorderTable you can at least prevent it from actually reordering position 1. There might be a better solution, but that's what my exhausted brain is coming up with You can find the reorderTable method in CanReorderRecords. You should be able to adapt that. Just tested it out and it's actually pretty simple. Just add:
if ($order[0] != 1) {
return;
}
if ($order[0] != 1) {
return;
}
This will allow you to reorder any rows, but if it's position 1 it'll snap back and won't reorder. You could even add a Filament notification there and let your users know they can't reorder position 1
ba_mbi_07
ba_mbi_07OP2y ago
yeah it is working if i am changing in vendor file but if i am adding this function in my list page it's not working
public function reorderTable(array $order): void
{
if ($order[0] != 1) {
return;
}
}
public function reorderTable(array $order): void
{
if ($order[0] != 1) {
return;
}
}
Dennis Koch
Dennis Koch2y ago
Can you share the full file?
Kenneth Sese
Kenneth Sese2y ago
Works for me. You need to include the entire code
public function reorderTable(array $order): void
{
if (! $this->isTableReorderable()) {
return;
}

if ($order[0] != 1) {
return;
}

$orderColumn = Str::afterLast($this->getTableReorderColumn(), '.');

$model = app($this->getTableModel());
$modelKeyName = $model->getKeyName();

$model
->newModelQuery()
->whereIn($modelKeyName, array_values($order))
->update([
$orderColumn => DB::raw(
'case ' . collect($order)
->map(fn ($recordKey, int $recordIndex): string => 'when ' . $modelKeyName . ' = ' . DB::getPdo()->quote($recordKey) . ' then ' . ($recordIndex + 1))
->implode(' ') . ' end'
),
]);
}
public function reorderTable(array $order): void
{
if (! $this->isTableReorderable()) {
return;
}

if ($order[0] != 1) {
return;
}

$orderColumn = Str::afterLast($this->getTableReorderColumn(), '.');

$model = app($this->getTableModel());
$modelKeyName = $model->getKeyName();

$model
->newModelQuery()
->whereIn($modelKeyName, array_values($order))
->update([
$orderColumn => DB::raw(
'case ' . collect($order)
->map(fn ($recordKey, int $recordIndex): string => 'when ' . $modelKeyName . ' = ' . DB::getPdo()->quote($recordKey) . ' then ' . ($recordIndex + 1))
->implode(' ') . ' end'
),
]);
}
I removed the relationship from this method And you need to import the Str and DB classes
ba_mbi_07
ba_mbi_07OP2y ago
yeah its worked even though i dont have to add entire code...i dont know why that time it is not working thanks
Kenneth Sese
Kenneth Sese2y ago
And don't forget to add a notification to let your users know why they can't sort position 1
ba_mbi_07
ba_mbi_07OP2y ago
Sure Thank you hey can you tell when you used this code do other rows can reorder ...in my case other rows cant be reordering with 1st row And in $order we are getting ids from table...so how can we use $order[0] ! = 1
Kenneth Sese
Kenneth Sese2y ago
Correct. Isn’t that the point? If you don’t want position 1 to be able to move, then that also means that other rows can’t move into position 1, right? Ok, I’m also assuming that “position” is your sort order column. Maybe position is a totally different column? Do you have a position column and a sort column?
ba_mbi_07
ba_mbi_07OP2y ago
No that is not happening ..what i mean is when i am reordering position between 2 and 3 or 3 and 4....they are also not being reordered no i have only position column i am not sorting any other column
Kenneth Sese
Kenneth Sese2y ago
Ok. Thanks for clarifying. All the other rows were sorting for me. Did you copy paste my code? And as I mentioned, I had removed the relationship part so if this is a relationship then that could be a reason And the reason this works is because the $order variable is just an ordered list of the new position numbers. dd($order) to see what I mean. It’s not a list of the model ids. So since position 1 should always be first (key 0 in the array), if there’s a different position number there then you know that position 1 was moved. Here you mentioned you didn’t need to use the entire code…try the entire code.
ba_mbi_07
ba_mbi_07OP2y ago
I tried entire code and with relation part also I think may be it's because you have newly created those...so primary key are starting from 1...may be you can delete some data then try reordering
Kenneth Sese
Kenneth Sese2y ago
I don't think that's the case, but let me check Still working. I'll send a video I apologize...you are right. Hold on @ba_mbi_07 Ok my apologies. I was wrong on what the $order was returning. It was returning the model's id. I was trying to do too many things at once. In this case I think you need to query the model and check if the model id in $order[0] is position 1 in the DB. Here's how I did it and now it does work:
public function reorderTable(array $order): void
{
if (! $this->isTableReorderable()) {
return;
}

$orderColumn = Str::afterLast($this->getTableReorderColumn(), '.');

$model = app($this->getTableModel());

if ($model->find($order[0])->sort !== 1) {
return;
}

$modelKeyName = $model->getKeyName();

$model
->newModelQuery()
->whereIn($modelKeyName, array_values($order))
->update([
$orderColumn => DB::raw(
'case ' . collect($order)
->map(fn ($recordKey, int $recordIndex): string => 'when ' . $modelKeyName . ' = ' . DB::getPdo()->quote($recordKey) . ' then ' . ($recordIndex + 1))
->implode(' ') . ' end'
),
]);
}
public function reorderTable(array $order): void
{
if (! $this->isTableReorderable()) {
return;
}

$orderColumn = Str::afterLast($this->getTableReorderColumn(), '.');

$model = app($this->getTableModel());

if ($model->find($order[0])->sort !== 1) {
return;
}

$modelKeyName = $model->getKeyName();

$model
->newModelQuery()
->whereIn($modelKeyName, array_values($order))
->update([
$orderColumn => DB::raw(
'case ' . collect($order)
->map(fn ($recordKey, int $recordIndex): string => 'when ' . $modelKeyName . ' = ' . DB::getPdo()->quote($recordKey) . ' then ' . ($recordIndex + 1))
->implode(' ') . ' end'
),
]);
}
Kenneth Sese
Kenneth Sese2y ago
Kenneth Sese
Kenneth Sese2y ago
You might be able to refactor that to avoid a separate DB call and only update it if the position isn't 1...but I'll leave that to you.
black ka1ser
black ka1ser2y ago
hi, this isnt related to the question but, if we modify anything in the vendor file, wont it go missing later (for example if my friend installs and runs the app on his ide)
Kenneth Sese
Kenneth Sese2y ago
Correct. Don't modify anything in the vendor file unless you want to just test something really quick. When you composer update or push to production you'll lose those changes. The above change isn't overriding a method in the vendor file, but inside their project. That you can safely do
black ka1ser
black ka1ser2y ago
any way to keep those changes eventho after composer update?
Kenneth Sese
Kenneth Sese2y ago
I mistyped above (fixed). The way to keep those changes is to override the method in your own ListXYZ class. @black13kaiser So using this help topic as an example, OP has a HomePageHandlersResource. There is also a ListHomePageHandlers class. The code I shared above would need to be copied into that ListHomePageHandlers class. This would then override the vendor's reorderTable method. Then you can safely composer update because you haven't touched the vendors file
awcodes
awcodes2y ago
@black13kaiser maybe checkout a refresher or tutorial on laracasts about OOP. That should help to make it more clear.
black ka1ser
black ka1ser2y ago
understood thanks!
awcodes
awcodes2y ago
Hope that didn’t sound condescending, it wasn’t meant that way.
black ka1ser
black ka1ser2y ago
nah, at least now i know about laracasts because the case here is im using a blog package, which creates three resources for me (blog, author and category). i created a custom page for reading the blog, and i added a read button at the vendor's blog resource only then i discovered that it wont be saved on production/ new install
awcodes
awcodes2y ago
Fair enough. Get it done. 👍
ba_mbi_07
ba_mbi_07OP16mo ago
its okay though..but I got the idea what i need to do from you yeah i did same way...Thank you for your help 😃 hey i need more help this I actually added my position 1 data to separate widget now when i am trying to reorder that its not reordering
Want results from more Discord servers?
Add your server