sleeping-owl / admin

Administrative interface builder for Laravel
http://sleeping-owl.github.io/
MIT License
504 stars 259 forks source link

Soft Deleted items are shown in admin panel #30

Closed vkosachev closed 9 years ago

vkosachev commented 9 years ago

Hi, so thats the question. How to hide the items that are soft deleted ? Is there an option for that available with model configuration? Thx

sleeping-owl commented 9 years ago

I've tried soft delete trait in model - deleted items doesn't display in table. It seems that it works fine in default case. Please provide some additional data.

vkosachev commented 9 years ago

Ok, Laravel 4.2 . I don't extend your model, I use mine: here's the basic config :

use Illuminate\Database\Eloquent\SoftDeletingTrait;

class Dare extends \BaseModel
{
    use SoftDeletingTrait;
    protected $dates =  ['deleted_at'];
//etc... 
}

The Admin Model configuration:

Admin::model('\\Dare')->title('Dares')->with('comments')->filters(function ()
{
    ModelItem::filter('flagged')->scope('flagged')->as('flagged')->title('Flagged');
    ModelItem::filter('finished')->scope('finished')->as('finished')->title('Finished');
})->denyEditing()->denyCreating()->columns(function ()
{
    Column::string('challenge', 'Challenge');
    Column::string('start_bet', 'Start bet');
    Column::string('pot', 'Pot');
    Column::count('comments', 'Comments')->append(Column::filter('dare_id')->model('\Comment'));
})->form(function ()
{

});

and thats it. I see that rows are being marked with deleted_at timestamp, but I still see them listed under admin section. Thx

sleeping-owl commented 9 years ago

I can't reproduce this bug, but try adding this to your model configuration:

Admin::model('\\Dare')->...->disableWithJoin()->...
vkosachev commented 9 years ago

Same case. I tried to remove the soft delete trait, but it seems that it doesn't see my deleting event handler :

    public static function boot()
    {
        parent::boot();

        static::deleting(function($dare)
        {
            \Transaction::where('dare_id', '=', $dare->id)->chunk(100, function($transactions) use ($dare)
            {
                foreach ($transactions as $transaction)
                {
                    $refund = Braintree_Transaction::refund($transaction);
                    if ($refund->sucess)
                    {
                        $transaction->status = 'refunded';
                        $transaction->save();
                    }
                    else
                    {
                        $transaction->status = $refund->transaction->processorSettlementResponseText;
                        $transaction->save();
                    }
                }
            });
            DB::table('comments')->where('dare_id', '=', $dare->id)->delete();
            DB::table('contributions')->where('dare_id', '=', $dare->id)->delete();
            DB::table('votes')->where('dare_id', '=', $dare->id)->delete();
            DB::table('likes')->where('dare_id', '=', $dare->id)->delete();
            DB::table('flags')->where('dare_id', '=', $dare->id)->delete();
            DB::table('notifications')->where('dare_id', '=', $dare->id)->delete();
            DB::table('tags')->where('dare_id', '=', $dare->id)->delete();
            if (!is_null($dare->path))
            {
                @unlink($dare->path);
            }
            $dare->delete();

        });
sleeping-owl commented 9 years ago

I added deleting event handler to my demo application: https://github.com/sleeping-owl/admin_demo/blob/master/app/models/Company.php#L14

Try to delete any company here (credentials is admin/SleepingOwl): http://sleeping-owl-admin-demo.gopagoda.com/admin/companies

My package uses the most common way to work with models. Here is the destroy method, it uses findOfFail and delete methods of your model and nothing else: https://github.com/sleeping-owl/admin/blob/master/src/SleepingOwl/Admin/Repositories/ModelRepository.php#L203

It seems like this problem is in your scripts.

tfevens commented 9 years ago

@sleeping-owl Regarding soft-deletes: I've not looked into it that much, but is there an easy way to view deleted items when using soft deletes? If so, perhaps swapping the delete button for a 'Restore' or something similar might be useful as well?

sleeping-owl commented 9 years ago

You can display deleted items with filters:

->filters(function ()
{
    ModelItem::filter('deleted')->scope('onlyTrashed')->title('deleted');
})

This filter will work on page admin/model?deleted.

I'll add 'Restore' button in future.