如何在具有软删除的表中使用子查询?

huangapple go评论49阅读模式
英文:

How to use a sub_query with a table that has soft delete?

问题

我有一个查询和一个子查询,看起来像这样:

// $query not included here
$query = $query->getQuery();
$subQuery = User::select('*')
    ->fromSub($query, 'sub_query')
    ->where($status, 'LIKE', '%' . $statusValue . '%')
    ->with($this->eagerLoadedRelationships)
    ->get();

但我收到以下错误消息:Column not found: 1054 Unknown column 'users.deleted_at' in 'where clause'。当我从我的User模型中移除use SoftDeletes;时,它可以正常工作。但我需要那个软删除功能。

我尝试过User::withoutTrashed()->select('*')...,但问题仍然存在。

有没有办法我可以解决这个问题?

我不能在select中列出列,而且我必须使用这个子查询来检查前一个查询的结果(我不能在前一个查询中添加where())。

英文:

I have a query and a subquery that looks like this:

// $query not included here
$query = $query->getQuery();
$subQuery = User::select('*')
    ->fromSub($query, 'sub_query')
    ->where($status, 'LIKE', '%' . $statusValue . '%')
    ->with($this->eagerLoadedRelationships)
    ->get();

But I get the following error: Column not found: 1054 Unknown column 'users.deleted_at' in 'where clause'. When I remove use SoftDeletes; in my User model, it works fine. But I need that soft delete.

I tried User::withoutTrashed()->select('*')... but it's the same.

Is there a way for me to go around this?

I can't list the columns inside select. And I kinda have to use this sub query to check the results of the previous query (I can't add where() to the previous query).

答案1

得分: 1

尝试以下代码:

$subQuery = User::select('*')
    ->fromSub($query, 'sub_query')
    ->where($status, 'LIKE', '%' . $statusValue . '%')
    ->whereNull('deleted_at')
    ->with($this->eagerLoadedRelationships)
    ->get();
英文:

Try following

$subQuery = User::select('*')
    ->fromSub($query, 'sub_query')
    ->where($status, 'LIKE', '%' . $statusValue . '%')
    ->whereNull('deleted_at')
    ->with($this->eagerLoadedRelationships)
    ->get();

答案2

得分: 0

$query = $query->getQuery();

// 使用 "toBase()" 来从子查询中移除全局作用域,包括软删除作用域
$subQuery = User::select('*')
->fromSub($query->toBase(), 'sub_query')
->where($status, 'LIKE', '%' . $statusValue . '%')
->with($this->eagerLoadedRelationships)
->get();

By calling toBase() on the subquery, you are essentially getting the underlying Query Builder instance without any global scopes applied, including the soft delete scope. This should allow you to run the subquery without encountering the "Column not found" error related to deleted_at.

英文:
$query = $query->getQuery();

// Use "toBase()" to remove the global scopes from the subquery, including soft delete scope
$subQuery = User::select('*')
    ->fromSub($query->toBase(), 'sub_query')
    ->where($status, 'LIKE', '%' . $statusValue . '%')
    ->with($this->eagerLoadedRelationships)
    ->get();

By calling toBase() on the subquery, you are essentially getting the underlying Query Builder instance without any global scopes applied, including the soft delete scope. This should allow you to run the subquery without encountering the "Column not found" error related to deleted_at.

huangapple
  • 本文由 发表于 2023年2月24日 00:51:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547886.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定