gpt4 book ai didi

php - 限制在 WhereHas 内

转载 作者:行者123 更新时间:2023-12-04 15:43:51 32 4
gpt4 key购买 nike

所以我想获得最后状态等于给定状态的请求我试过 first() 但它给了我一个错误并且限制不起作用

$requests = Request::whereHas('requestStatus', function ($query) use ($status) {
return $query->where('status',$status)->orderBy('updated_at', 'desc')->limit(1);
})->get();

Controller

    public function listByStatus($status)
{

$requests = Request::whereHas('requestStatus', function ($query) use ($status) {
return $query->where('status',$status)->orderBy('updated_at', 'desc')->limit(1);
})->get();


return view('admin.request.list',compact('requests','status'));
}

模型请求

public function requestStatus()
{
return $this->hasMany('App\RequestStatus');
}

迁移请求状态

Schema::create('request_status', function (Blueprint $table) {
$table->increments('id');
$table->integer('request_id')->unsigned();;
$table->foreign('request_id')->references('id')->on('requests');
$table->string('status');
$table->string('image')->nullable();
$table->timestamps();
});

最佳答案

试试这个:

$requests = Request::select('requests.*')
->join('request_status', 'requests.id', 'request_status.request_id')
->where('request_status.status', $status)
->where('request_status.id', function($query) {
$query->select('id')
->from('request_status')
->whereColumn('request_id', 'requests.id')
->orderByDesc('updated_at')
->limit(1);
})
->get();

关于php - 限制在 WhereHas 内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50271514/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com