Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pickab00's avatar

Calling Method in Model as an API call

So I have a custom method which checks if the restaurant is open or not. Now I have implemented it in laravel successfully by doing something like @if($restaurant->opened()) which returns a true else it returns a false. Now how can I do the same and apply this on to an API return inside of a json.

Here is my method:

public function opened(){
       
        $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();
            if ($current_time > $todays_timings->open_time && $current_time < $todays_timings->close_time){
                return true;
            }
        }

    }

It's a big mess but basically it returns true or false. And in my API I am doing this:

//Simply
$restaurant = Restaurant::where('active', 1)->latest()->get();

So I am expecting a json which would return all the restaurants which meets the condition. Now how can I apply something like restaurant_open: 1 or something in my json return with the above method in my model

0 likes
4 replies
Nakov's avatar
Nakov
Best Answer
Level 73

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

https://laravel.com/docs/5.8/eloquent-resources

1 like
pickab00's avatar

Hi, I am getting "Call to undefined relationship [opened] on model [App\Restaurant]." error. When I use protected $with = [ 'opened' ];

Nakov's avatar

@pickab00 sorry I gave you the wrong property, it should be this one:

protected $appends = [ 'opened'];

Sorry about that.

1 like
pickab00's avatar

Genius. This works great. Thank you so so much!

And also thank you for the shortened version of my code :)

Please or to participate in this conversation.