First of all your method returns just true but never false so add as a last line in your method: return false; to be explicit.
Then you can make it as an accessor instead of just a method.. like this:
public function getOpenedAttribute()
{
$today = Carbon::today()->format('l');
$today_exists = $this->timings()->where('day', $today)->exists();
$current_time = Carbon::now()->format('H:i');
if ($today_exists){
$todays_timings = $this->timings()->where('day', $today)->first();
return $current_time > $todays_timings->open_time && $current_time < $todays_timings->close_time;
}
return false;
}
Then also in the same model add this at the top:
protected $with = [ 'opened' ];
This will include that attribute always when you load an instance of the model.
And to use it instead of
$restaurant->opened()
You will use it as a property
$restaurant->opened
Note I simplified it a bit :)
Let me know if it works :)
Edit as an even better option maybe for API is to create a resource and return that as a response more on that in the docs