I have an Eloquent model, that morphs to other Eloquent models, like this:
<?php
class Message extends Eloquent {
public function message()
{
return $this->morphTo('message');
}
}
The reason being, the application has many different types of messages. I know it’s not the most elegant of approaches, but it’s what I’ve been lumbered with by the previous developer.
So, my issue is: I need to only return Message
instances, where the state
column in the morphed-to message table matches a value. I have this query scope method in my Message
model class:
public function scopeDeclinedMessages($query)
{
return $query->with(array('message' => function ($q)
{
$q->where('state', '=', MessageState::DECLINED);
}));
}
However, this doesn’t work, as it still returns Messages
where the state
in the morphed-to table doesn’t match the declined state.
For transparency, MessageState::DECLINED
is a value of 2
, but I’m getting messages back where the morphed-to table state
column equals 0
.