Laravel 历史记录页面和软删除

问题描述 投票:0回答:1

我想在项目完成后将项目数据保存在历史页面中,并在同一页面中进行软删除功能。但它只显示状态为完成的数据

class HistoryController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $projects = Project::where('status', 'Done') //select data with done status only
                    ->orWhereNotNull('deleted_at') // shows soft deleted data
                    ->orderBy('id', 'desc')
                    ->paginate(5);

        // Pass the merged projects to your view
        return view('history', compact('projects'));
    }
}

我尝试有 2 个变量然后将其合并。它确实有效,但它使分页不起作用。我也尝试了这个,但结果是一样的

public function index()
    {
        $projects = Project::withTrashed()
                            ->where('status', 'Done')
                            ->orderBy('id', 'desc')
                            ->paginate(5);

        // Pass the merged projects to your view
        return view('history', compact('projects'));
    }

可以这样做吗?

php laravel eloquent laravel-10 soft-delete
1个回答
0
投票

您的代码不起作用,因为它尝试查找状态=“完成”的项目。如果有任何项目没有此状态但具有非空“deleted_at”值,则由于第一个“WHERE”子句,它们不会被捕获。

尝试条件查询分组,它将选择“status”为“Done”和“deleted_at”为空的两个项目。

$projects = Project::withTrashed()
                   ->where(function ($query) {
                       $query->where('status', 'Done');
                       $query->orWhereNotNull('deleted_at');
                   })
                   ->orderBy('id', 'desc')
                   ->paginate(5);
© www.soinside.com 2019 - 2024. All rights reserved.