如何 return 对象而不是数组中的对象

how to return object instead of object in array

我正在尝试 return 由于 hasMany 关系而来自集合数组的对象的响应。

我试过return$block->where('date','=',$today)->first();

error said: Call to undefined method App\BlockDate::addEagerConstraints()

public function block_dates() 
{
    return $this->hasMany(BlockDate::class);
}

public function schedule_block() 
{
    $today = Carbon::today()->toDateString();
    $block = $this->block_dates();

    return $block->where('date','=',$today)->first();
}

schedule_block() 应该 return BlockDate 的对象。如果我删除 first(),它 return 是一个包含所需对象的数组。我只想根据关系检索对象。感谢任何帮助。

试试这个:

public function schedule_block() {
    $today = Carbon::today()->toDateString();
    return $this->hasOne(BlockDate::class)->where('date','=',$today);
}