Reputation: 295
I am trying to return a response of an object which came from a collection array due to a relation of hasMany.
I have tried to do a 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()
should return an object of BlockDate
. If I remove first()
, it returns an array with the desired object in. I would like to just retrieve the object based on the relation. Any help is appreciated.
Upvotes: 1
Views: 927
Reputation: 13669
try this one :
public function schedule_block() {
$today = Carbon::today()->toDateString();
return $this->hasOne(BlockDate::class)->where('date','=',$today);
}
Upvotes: 2