php - 搜索 Laravel 等价物
问题描述
我是初学者网络开发人员。我的模型有小问题。
我有这个迁移和模型:
Schema::create('stopwatches', function (Blueprint $table) {
$table->increments('id');
$table->integer('case_id')->unsigned();
$table->foreign('case_id')->references('id')->on('case_instances')->onDelete('cascade');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
});
Schema::create('timecycles', function (Blueprint $table) {
$table->increments('id');
$table->integer('timecycleable_id');
$table->string('timecycleable_type');
$table->integer('case_id')->unsigned();
$table->foreign('case_id')->references('id')->on('case_instances')->onDelete('cascade');
$table->boolean('status')->default(0);
$table->integer('worked_time')->default(0);
$table->timestamps();
});
class Stopwatch extends Model
{
protected $fillable = [
'case_id',
'user_id'
];
protected $casts = [
'id' => 'int',
'case_id' => 'int',
'user_id' => 'int',
];
protected $dates = [
'created_at',
'updated_at'
];
public function timeCycle()
{
return $this->morphMany(Timecycle::class, 'timecycleable');
}
public function caseInstance()
{
return $this->belongsTo(CaseInstance::class, 'case_id');
}
class Timecycle extends Model
{
protected $fillable = [
'case_id',
'timecycleable_id',
'timecycleable_type',
'status',
'worked_time'
];
protected $casts = [
'id' => 'int',
'case_id' => 'int',
];
protected $dates = [
'created_at',
'updated_at'
];
public function stopwatch()
{
return $this->morphTo();
}
}
在时间周期中,我有 2 条状态 = 0 和状态 = 1 的记录:https ://ibb.co/5vyh316
我需要只有一个时间周期的秒表,状态 = 1。
我做这个代码:
return $this->stopwatch::with(['timeCycle', 'caseInstance'])
->where('user_id', Auth()->user()->id)
->where('updated_at', '<=', \Carbon\Carbon::now()->subSeconds(30)->toDateTimeString())
->whereHas('timeCycle', function ($q) {
$q->where('status', 1);
})
->get();
结果我有展位物品:https ://ibb.co/WnJ58Kc
我的代码有什么问题?
我该如何修复它?
请帮我。
解决方案
问题很可能是由该with('timeCycle')
子句产生的。如果你想通过一个关系来约束你的查询,你需要在whereHas
和中都指定它with()
,否则急切加载将获取所有whereHas
无关紧要的元素。尝试这个:
return $this
->stopwatch::with([
'caseInstance',
'timeCycle' => function ($query) {
$query->where('status', 1);
}
])
->where('user_id', Auth()->user()->id)
->where('updated_at', '<=', now()->subSeconds(30)->toDateTimeString())
->whereHas('timeCycle', function ($query) {
$query->where('status', 1);
})
->get();
当然,假设两个约束相等,您可以将其存储在变量中并将其传递给两个方法:
$timeCycleContraint = function ($query) {
$query->where('status', 1);
};
return $this
->stopwatch::with([
'caseInstance',
'timeCycle' => $timeCycleContraint
])
->where('user_id', Auth()->user()->id)
->where('updated_at', '<=', now()->subSeconds(30)->toDateTimeString())
->whereHas('timeCycle', $timeCycleContraint)
->get();
推荐阅读
- java - OpenJDK 11 和 macOS 不支持 ZGC?
- powerbi - 在 Power BI 中显示相同值的所有行和行总数
- mysql - 复杂的sql查询 - 无法弄清楚
- sql - 使用 SQL 过程返回表?
- sql - 将具有相似标识符的两行数据相加
- mulesoft - 嵌套数组列表上的 dataweave 过滤器和 maxBy
- java - 从命令行编译 Java '找不到符号'
- javascript - 如何在 React 渲染中无限循环数组?
- javascript - 如何使用订阅将对象推送到可观察对象?
- ios - 使用 CloudKit 查询以及 tableview 方法 (numberOfRowsInSection) 可能被调用的次数无法预测的事实