skip to Main Content

i wanna get the reservations that their status aren’t canceled or if status is canceled it check they have unread messages or not

Can you help me?

class Reservation extends Model
{
  public function unreadMessages() {
      return $this->hasMany(Message::class, 'reservation_id', 'id')
               ->where('read', 0);
  }
}

2

Answers


  1. use where callback method

    Reservation::where(function ($query) {
            $query->where('status','!=','canceled')
                ->orWhere(function ($query){
                    $query->where('status','canceled')->whereHas('unreadMessages');
                });
        })->get();
    
    Login or Signup to reply.
  2. try this

    $data = User::with(['unreadMessages'=> function($q){
                $q->where('read', 0);
            }])->get();
    

    Remove the condition for the model and implement it in the way mentioned in the query.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search