如果活动有可接受的优惠,请检查功能?

I need to check if an event has an offer and i have made this function

public function hasAcceptedOffer()
{
    foreach ($this->offers as $offer) {

        if( $offer->accepted == 1 ){
        return true;

    } else {

        return false;

    }
}

But i think this can be made better, more optimized. Because if there are a lot of offers i don't want to go through all of them. What i want is that if a function finds an offer that is accepted it should stop further iteration.

I think you did good here.

You can remove the else part by doing this

public function hasAcceptedOffer()
{
    foreach ($this->offers as $offer) {

        if( $offer->accepted == 1 ){
           return true;

        }
    }
    return false;
 }

Assuming you only care if any offer was accepted, and you don't need to know which one, I like to do it like this:

public function hasAcceptedOffer()
{
    foreach($this->offers as $offer) {
        if($offer->accepted !== 1)
            continue;

        return true;
    }

    return false;
}

Each iteration of the loop will quickly skip to the next one if it doesn't match your desired criteria, it will return immediately when it finds the first positive result and skip processing the others (since you don't care anyway) and return false if no matches are found.

If you want to collect the offers that were accepted, you can amend it like so:

public function getAcceptedOffers()
{
    $results = array();

    foreach($this->offers as $offer) {
        if($offer->accepted !== 1)
            continue;

        $results[] = $offer;
    }

    return $results;
}