首页 > 解决方案 > 搜索 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

我的代码有什么问题?

我该如何修复它?

请帮我。

标签: phplaravel

解决方案


问题很可能是由该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();

推荐阅读